Archived

This topic is now archived and is closed to further replies.

Facehat

Finding Memory Leaks

Recommended Posts

I''ve heard of some programs which can find memory leaks C++ code. Does anyone have any reccomendations about which one of these I should get? And what sort of price can I expect to pay? --TheGoop

Share this post


Link to post
Share on other sites
Boehm''s Garbage collection library is avaliable from Xerox for free, and can perform leak detection as well as garbage collection. (Because, after all, every time you need to collect the garbage, you''ve got a leak. )

Share this post


Link to post
Share on other sites
Here''s a Xerox ftp directory: ftp://parcftp.xerox.com/pub/gc/

But aparantly since the last time I checked, Boehm had moved from Xerox to SGI to HP. Here''s the HP link, I didn''t check if the library was actually more recent than the Xerox Parc link.

http://www.hpl.hp.com/personal/Hans_Boehm/gc/

Share this post


Link to post
Share on other sites
If you run in debug mode, you can just use the _Crt functions to detect any over/undrwrite, memoryleaks and other useful information from the debug heap. Easy to use, for example run _CrtDumpMemoryLeaks() at the end of your program.

/ LC

Share this post


Link to post
Share on other sites
I read in VC++ Developers Journal a while ago that _CrtDumpMemoryLeaks will give incorrect results if you include header files that declare objects, such as iostream.h (declared cout, cin, etc..). It said that it will return the size of those objects, because their destructors haven''t been run yet. It this true?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Yea you can use the _Crt functions. Try lookin up _CrtSetDbgFlag. int _CrtSetDbgFlag( int newFlag );

here''s a snippet out of MSDN
1. Call _CrtSetDbgFlag with newFlag equal to _CRTDBG_REPORT_FLAG to obtain the current _crtDbgFlag state and store the returned value in a temporary variable.

2.Turn on any bits by OR-ing the temporary variable with the corresponding bitmasks (represented in the application code by manifest constants).

3.Turn off the other bits by AND-ing the variable with a bitwise NOT of the appropriate bitmasks.

4.Call _CrtSetDbgFlag with newFlag equal to the value stored in the temporary variable to set the new state for _crtDbgFlag.

and a flag of interest for detecting memory leaks upon exit is _CRTDBG_LEAK_CHECK_DF.
This will basically call the _CrtDumpMemoryLeaks() upon exit in debug mode. Hope this helps ya out.

Share this post


Link to post
Share on other sites
Qoy - I had those problems..So I would say it''s true. I logged it to the hd and got a 81k file with all the leaks in my application. Think I had any? Nah.

"Paranoia is the belief in a hidden order behind the visible." - Anonymous

Share this post


Link to post
Share on other sites
One thing I''ve done to help track memory leaks was to override the new and delete operators to make them count the number of bytes allocated and deallocated. You may also want to look into creating your own memory manager that allocates memory in large blocks - this way you can help prevent fragmentation (or at least control w/ your own routines) and you simply deallocate all the blocks at the end. Just some ideas...
- Kane THP

"Feed the Children, Save the Whales, Free the Mallocs!"

Share this post


Link to post
Share on other sites