Detecting leaks in your library

Started by
6 comments, last by Sneftel 14 years, 2 months ago
I am trying to figure out a way to have memory leak detection in my library. For my standard applications I use MMGR to find out what line my memory leak is on. How could I do this for a library I am creating?
Advertisement
Maybe not exactly what you are looking for, but if it's a memory-related bug, Valgrind is great.
Quote:Original post by u
Maybe not exactly what you are looking for, but if it's a memory-related bug, Valgrind is great.


Valgrind is not windows supported. I am looking for something Window supported (I did not detail that earlier).
Assuming you're building under MSVC, I've had great success with its built-in leak checking.
Quote:Original post by Sneftel
Assuming you're building under MSVC, I've had great success with its built-in leak checking.


Maybe there is some sort of guide I am missing, because at most I get

{5947} normal block at 0x0203D938, 432 bytes long.
Data: <0 3 3 > 30 E8 33 02 D4 F6 33 02 00 00 00 00 CD CD CD CD

Is there a war to show line numbers? Could you explain how much more detail it gives?
By default the debug runtime leak check does not produce file/line information for memory allocated with new, only for malloc.

Try this in each .cpp file you want to detect leaks from (you can make it a include file called DebugNew.h or something.) It needs to be placed after all the includes you need, but before your actual code:

#ifdef _DEBUG

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

#define _DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)

#define new _DEBUG_NEW

#endif
I've been using Visual Leak Detector for awhile now. I am not sure if it is Windows only but I know it works with Windows and Visual Studio. Just include a header, and viola shows you a stack trace of where ever you leak memory. Can also show duplicates (though I turn this feature off).

I believe it might but Microsoft specific, but I haven't read the documentation in a long time. You need to run using F5, and it compiles out during release. Running with Shift-F5 does not attach the debugger and VLD won't work either.

Hope that helps. Happy Coding.
Quote:Original post by simotix
Is there a war to show line numbers? Could you explain how much more detail it gives?

As AgentC said, you can attach file/line information. Alternatively, assuming you can run the library deterministically, you can identify a leaked block by number, then re-run the program and break on that allocation.

Furthermore, RTFM.

This topic is closed to new replies.

Advertisement