Memory leak in MemeFighter code- Intermediate 18

The Partridge Family were neither partridges nor a family. Discuss.
MCanterel
Posts: 10
Joined: December 8th, 2017, 8:48 pm

Memory leak in MemeFighter code- Intermediate 18

Post by MCanterel » December 29th, 2017, 10:59 pm

Hi All.
Anyone notice a memory leak in the MemeFighter code from I-18?
When I #include <crtdbg.h> and run _CrtDumpMemoryLeaks(); I get 4 leaked objects.
However, cuz I'm new to all this, despite #define _CRTDBG_MAP_ALLOC and redefining the 'new' operator (per a bunch of StackOverflow posts), I can't coax _CrtDumpMemoryLeaks into dumping the file and line number of the leaks, so I'm kinda stuck.
Soooooo, anyone have an insight about how to go about investigating a memory leak like this?
Thanks, M

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Memory leak in MemeFighter code- Intermediate 18

Post by albinopapa » December 30th, 2017, 1:46 am

Post your project, perhaps that would bee a better option than shots in the dark.

Best way would be to use GitHub, if you followed chili's videos.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

MCanterel
Posts: 10
Joined: December 8th, 2017, 8:48 pm

Re: Memory leak in MemeFighter code- Intermediate 18

Post by MCanterel » December 30th, 2017, 2:33 am

Not really shots in the dark-- just added that to the master branch of chili's repo. But gimme a sec and I'll share my repo.

MCanterel
Posts: 10
Joined: December 8th, 2017, 8:48 pm

Re: Memory leak in MemeFighter code- Intermediate 18

Post by MCanterel » December 30th, 2017, 2:51 am

Here's my repo with the <crtdbg.h> code added:

https://github.com/MCanterel/Inheritance.git

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Memory leak in MemeFighter code- Intermediate 18

Post by albinopapa » December 30th, 2017, 12:30 pm

Nope, not a leak. The t1 and t2 vectors are still "alive" when _CrtDumpMemoryLeaks() is called. Put lines 315 - 372 inside curly braces {} and the two vectors "die" at the } online 372 and no dump is output to the console.

The way I was able to get that is the output showed the pointers that were still alive. Then I checked the addresses of t1 and t2 and t1 had an address that was pretty close ( within 64 bytes ) so I just enclosed in {} and no more mem leak dump.

This approach would be impossible to do in a larger project. I had this issue with some global smart pointers, and it took quite some time to track down the culprits.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

MCanterel
Posts: 10
Joined: December 8th, 2017, 8:48 pm

Re: Memory leak in MemeFighter code- Intermediate 18

Post by MCanterel » December 30th, 2017, 4:26 pm

Wow, yeah: braces = scope. Duh. Thanks Albi.
The address of the allocator for the t1 vector was exactly the one ref'd in _CrtDumpMemoryLeaks()!
But wait, are vectors allocated in the stack or the heap?
Also, dumb question: how do I add a screencap to these posts?
Thx, M

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Memory leak in MemeFighter code- Intermediate 18

Post by Yumtard » December 30th, 2017, 5:43 pm

The elements of a vector are stored on the heap

MCanterel
Posts: 10
Joined: December 8th, 2017, 8:48 pm

Re: Memory leak in MemeFighter code- Intermediate 18

Post by MCanterel » December 30th, 2017, 7:55 pm

I needs me a red pill baby.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Memory leak in MemeFighter code- Intermediate 18

Post by albinopapa » December 31st, 2017, 6:13 am

Yeah, vector can be implemented as a base pointer and end pointer where size is ( end - begin ) / sizeof(T) and a reserved or capacity variable to determine when the container needs to grow. It can also be implemented as a base pointer with a size and reserved or capacity variables. The base pointer and end pointer and the capacity value are stored on the stack or base pointer and size and capacity values are stored on the stack, but as Yumtard said, the base/end pointers point to a location on the heap.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Memory leak in MemeFighter code- Intermediate 18

Post by chili » December 31st, 2017, 8:40 pm

I have been accused of leaking memory??!

Who dares besmirch the good name of ChiliTomatoNoodle??! D:<
Chili

Post Reply