
How to hunt memory leaks using Visual Studio
April 29, 2007Make sure you compile debug version of your project.
Press F5 (Start Debugging)
Do whatever you suspect to leak memory, and close program.
In output window you should see:
Detected memory leaks!
Dumping objects ->
{8677} normal block at 0x01AA4E08, 68 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.
Add:
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#define _INC_MALLOC
#endif
in your stdafx.h file, and recompile whole solution.
Now it should be something like:
Detected memory leaks!
Dumping objects ->
C:\Program Files\Microsoft Visual Studio 8\VC\include\crtdbg.h(1147) : {8677} normal block at 0x01AA4E08, 68 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.
The problem is it shows allocator code, not your allocation itself. Still not very usefull. Now tricky part. Double click
C:\Program Files\Microsoft Visual Studio 8\VC\include\crtdbg.h
line. Set a trap (F9/Debug->Toggle Breakpoint) right click on the red dot and select condition and type _Size==<your leak size in this case 68>. Next time you will debug, program will stop if there will be 68 bytes allocated, then you can select Debug->Windows->Call Stack and if you are lucky you will see your memory leak.
Project news:
- fixed memory leaks
- wxWidgets upgraded to 2.8.3
- fixed hierarchy while 3ds loading
- about 40% memory saved (scene class)
- accurancy upgraded from float to double in most cases
- Material and Texture class refactored
Some test images:
This problem had me going for 2 days. Solution:
Include the following definition so that the linker routes the new function to DEBUG_NEW and the unwrapper of the compiler will reveal the actual line number of code where the leak happens.
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
You will get a dump like:
Dumping objects ->
c:\test\test.cpp(194) : {137} normal block at 0×00369D40, 4 bytes long.
and NOT:
Dumping objects ->
C:\Program Files\Microsoft Visual Studio 8\VC\include\crtdbg.h(1147)