Finding a memory leak

Started by
2 comments, last by Davaris 22 years, 12 months ago
Hi guys, I''ve been working on this game engine of mine for 2 years now so there is a lot of code in there. The problem is it crashes after running for about 20-30 mins. I suspect a memory leak. The debugger won''t kick in so I can''t be sure. Are there any helpful functions in Vis C++ that allow you track memory allocations? Ideally they would tell me what was grabbed and where in the source. Thanks
"I am a pitbull on the pantleg of opportunity."George W. Bush
Advertisement
I think a lot of what I''m going to say is covered by the debug version of the VC library malloc/free, but not all, so...

if your code is in straight C you can #define malloc and free to a debug version (debug_malloc for example) which takes a couple of extra arguments (which the #define adds), thus...

void *debug_malloc (size_t Size, const char *File, int LineNumber)

#define malloc(_size) debug_malloc (_size, __FILE__, __LINE__)

your debug version takes these extra arguments are stores them some where (in a linked list?). You then write another function which dumps out the entire list of allocated memory to a debug file. This will allow you to see where a block was allocated from and free''d.

If you cause the debug dumper to be called from a state where all the memory should have been free''d, the debug file should only contain the leaking memory blocks.

Writing these functions can be time consuming, but ultimately is very worth while - especially if you add extra code to check for blocks having memory overruns (writing to more bytes than were alloced).

Something similar can be done by overloading new/delete in c++.

The book "Writing Solid Code" by Microsoft Press covers this a lot and is actually very useful, and quite an interesting read.
Overloading new/delete can indeed be done but it''s a slight bit more tricky, consult flipcode.com if you can''t figure out how - it''s all pre-processor magic .
Overloading new and delete also resulted in some problems with Dinkumware''s STL implementation (read MSVC STL ), took me some time and quite some patience to figure out I had to include all the STL headers before my kickass_memleak.h.

Also, if you''re only going to develop for Windows (not good, not advisable ) you can use crtDumpMemoryLeaks (???), I cannot recall the exact function name however. Which also caused me some problems by saying my STL containers had leaks in them while the problem was their destructors was called after I dumped the leaks (or so I think...whatever).

"This album was written, recorded and edited at Gröndal, Stockholm in the year of 2000. At this point in time money still ruled the world. Capitalistic thoughts were wide spread. From the sky filled with the fumes of a billionarie''s cigar to the deepest abyss drenched in nuclear waste. A rich kid was a happy kid, oh..dirty, filthy times. Let this be a reminder."
- Fireside, taken from back of the Elite album
here''s an article i found at flipcode a while ago, it''s really great if you''re using MSVC

Detecting Memory Leaks

shows all memory leaks in your debug/output window and even let''s you dblclick on a line in the output to jump straight to the line in your source code

This topic is closed to new replies.

Advertisement