Sign in to follow this  
simotix

Detecting leaks in your library

Recommended Posts

simotix    142
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?

Share this post


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

Share this post


Link to post
Share on other sites
Sneftel    1788
Assuming you're building under MSVC, I've had great success with its built-in leak checking.

Share this post


Link to post
Share on other sites
simotix    142
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?

Share this post


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

Share this post


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

Share this post


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

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