Sign in to follow this  
Nash

Detecting Memory Leaks

Recommended Posts

I'm trying to track down a memory leak. I tried a demo version of this memory tool but I couldn't get to work properly. I noticed Visual C++ has its own internal memory leak detecting routines. I called _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF) to display memory info on any exit point. I also used _CrtSetReportMode to display it to a message window to run my app outside of the debugger. However, I do not see any reports. Does the report only generate when it detects a memory leak?? Perhaps I have to run it inside the debugger?? Any help would be great. Thanks.

Share this post


Link to post
Share on other sites
If you run your game/application through the VC debugger(Using F5) and exit, it will dump a list of memory leaks in the output window.

If you want to be sure wether or not you are using the output window, try writing to it using OutputDebugString() just before you dump the mem leaks.

And yes, it only dumps a list when you have mem leaks.

Toolmaker

Share this post


Link to post
Share on other sites
Thanks. I tried it but my output is little weak. I tried the _CRTDBG_MAP_ALLOC def but it caused strange compile conflicts. However, without it, the memory leak data output is all greek. I'll have to use _CrtSetBreakAlloc() to track it down in file. Thanks again.

Share this post


Link to post
Share on other sites
To get file and line-number data on the reported memory leaks, you need to include something like:
#include <crtdbg.h>
#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
// If you use malloc/etc:
#define malloc(s) _malloc_dbg(s, _NORMAL_BLOCK, __FILE__, __LINE__)
#define calloc(c, s) _calloc_dbg(c, s, _NORMAL_BLOCK, __FILE__, __LINE__)
#define realloc(p, s) _realloc_dbg(p, s, _NORMAL_BLOCK, __FILE__, __LINE__)
#define free(p) _free_dbg(p, _NORMAL_BLOCK)


near the beginning of every .cpp file. (That's basically what _CRTDBG_MAP_ALLOC does, except for _CRTDBG_MAP_ALLOC not working with 'new'). It causes conflicts with some STL headers, so you have to include all of them before redefining 'new', but it should then give vaguely useful information like "c:\etc\file.cpp(55) : {125} normal block at 0x002F63F0, 4 bytes long."

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this