Sign in to follow this  
GhostfromTexas

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

Recommended Posts

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...

Share this post


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

Share this post


Link to post
Share on other sites
[quote name='gekko' timestamp='1305488532' post='4811188']
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.
[/quote]

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

Share this post


Link to post
Share on other sites
[quote name='GhostfromTexas' timestamp='1305498761' post='4811242']
#define new new( _NORMAL_BLOCK, __FILE__, __LINE__)
[/quote]

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

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