[C++, VS 2010 Ultimate] Advanced memory leak detection

Started by
2 comments, last by marius1930 12 years, 11 months ago
I am having a hard time tracking down some memory leaks in my program.. I am using the CRT visual c++ debugger, which is great for finding memory leaks that have consistant allocation numbers. But these memory leaks have different allocation numbers each time the program is run, so it's quite difficult to find what allocation is causing the leak.

Is there a way to log all memory allocations so I can go back later and figure out which one specifically happened for the particular time I ran the program? Or some better method of solving the memory leak issue?

Thanks in advance...
Advertisement
The quick answer is use another tool, like Visual Leak Detector and it will give you the location of all your leaks in the output window when the program exists.
-- gekko

The quick answer is use another tool, like Visual Leak Detector and it will give you the location of all your leaks in the output window when the program exists.


I looked into VLD but couldn't get it to give me anymore information than I already knew...

But however I did figure out a way to make CRT work better for me thanks from some help from someone..

global header. either a forced header in visual studio or one that's included everywhere in the project by some sort of method. In Visual Studio it's under Project Settings -> c++ -> Advanced -> Forced Include File (just list the file there without quotes. This assumes you own file, not a standard library include)
//--- CRT debug header (must be last)
#if defined(DEBUG) | defined(_DEBUG)
#include <crtdbg.h>
#define new new( _NORMAL_BLOCK, __FILE__, __LINE__)
#pragma warning(disable : 4345) //this warning is when using empty () when allocating a new object. Only happens when allocating memory with this debug option, so disabling it
#endif


first line in main:
#if defined(DEBUG) | defined(_DEBUG)
_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG)|_CRTDBG_LEAK_CHECK_DF);
//_CrtSetBreakAlloc(100); //break on a specific memory allocation
#endif


This allowed visual studio to output file and line number for the leaked allocation. The output looks something like this now:

c:\users\user\desktop\project-folder\files\file.cpp(392) : {487594} normal block at 0x087F1500, 456 bytes long.
Data: < fff?o : > CD CD CD CD 66 66 66 3F 6F 12 03 3A CD CD CD CD
{487590} normal block at 0x087F14B8, 8 bytes long.
Data: < > 1C 13 7F 08 00 00 00 00
c:\users\user\desktop\project-folder\files\file.cpp(275) : {487585} normal block at 0x087F12E0, 100 bytes long.
Data: < { > 20 14 7B 08 00 00 00 00 00 00 00 00 00 00 00 00
c:\users\user\desktop\project-folder\files\file.cpp(133) : {487578} normal block at 0x087E5038, 96 bytes long.
Data: < ;& > B8 3B 26 02 01 00 00 00 00 00 00 00 02 00 00 00

So even though i was getting random leaks, this method allow me to figured out how to track them down much easier.

Hopefully this helps some others in the future on how to fix it.

Thanks to Ghaleon from #gamedev on the #efnet IRC for this. He's the one that came up with the code

#define new new( _NORMAL_BLOCK, __FILE__, __LINE__)


Exactly what i needed, never had line numbers working.
Thanks

This topic is closed to new replies.

Advertisement