Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Finding a memory leak

This topic is 6295 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
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

Share this post

Link to post
Share on other sites
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

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!