memory leaks

Started by
3 comments, last by Vich 17 years, 9 months ago
In c++ is there any efficient way to find memory leaks in a large project? Perhaps some programm that matches new and deletes? -CProgrammer
Advertisement
You could look at MMGR for an intrusive solution, or CRT Debug Heap (Windows) for a less intrusive code solution.
If you are on Linux, then Valgrind can help.
I'm sure there are more options.


jfl.
Another way is to override new and delete. That way you can keep track of everything that is in memory by putting it in a memory manager class. When that class gets destructed at the end of the program, it should be empty. If it's not empty, you can start outputting debug messages or even throw a breakpoint:

#if defined( __WIN32__ ) || defined ( _WIN32 )#	define BreakHere __asm { int 3 }#else#	define BreakHere asm("break")#endif// Call "BreakHere;" to throw a breakpoint. Make sure to only do that in debug mode.



There's lots of stuff on Google about overriding new and delete.

Also, if you're using the GCC compiler, there's already a profile delivered, which also reports memory leaks.
[www.LifeIsDigital.net - My open source projects and articles.
Quote:
// Call "BreakHere;" to throw a breakpoint. Make sure to only do that in debug mode.

That's why you should either wrap up your interrupt in "#ifdef _DEBUG" or better yet, use _CrtDbgBreak from the CrtDbg header.
Quote:Original post by raz0r
Quote:
// Call "BreakHere;" to throw a breakpoint. Make sure to only do that in debug mode.

That's why you should either wrap up your interrupt in "#ifdef _DEBUG" or better yet, use _CrtDbgBreak from the CrtDbg header.


Of course, this was just the core of that code :)
Using _CrtDbgBreak is no-go for me, since it only works in Windows and I need stuff to work for Linux and ARM(Nintendo DS) too.
[www.LifeIsDigital.net - My open source projects and articles.

This topic is closed to new replies.

Advertisement