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 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 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 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 on other sites
There's an 'industrial strength' memory manager presented on Flipcode's Ask Midnight column. It may be useful to you. Also 'How to Find Memory Leaks', also on Flipcode.

Create an account

Register a new account

• Forum Statistics

• Total Topics
627757
• Total Posts
2978950

• 11
• 10
• 10
• 23
• 14