#### Archived

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

# Memmory Leak Tools

## Recommended Posts

Prozak    898
What tools can I use to know if my application/ game is leaking memmory? In DOS, I would just do a "mem", run the program then do another "mem". If there was any diference, then I would know that there was a memory leak somewhere. How do I track this problem down in VC++?

[Hugo Ferreira][Positronic Dreams][]
"Research is what I''m doing when I don''t know what I''m doing."
- Wernher Von Braun (1912-1977)

##### Share on other sites
z b    122

Check out the "_CrtCheckMemory" family of functions. There is sample code, etc.. on MSDN.

-z

##### Share on other sites
zipless    122
The best way i''ve found is to overload the new&delete or alloc and free functions.

Something like this:

#define malloc(size) (mymalloc(size.__FILE__,__LINE__))

then for every alloc put the details into a linked list,
when you free search the list and remove the related memory.

Now at the end of your programm after everything has been released write the contents of the list to a text file, open it and you not only see how much memory you have unreleased but also in what file at what line it was declared.

Checkout flipcode, i think they have a pretty decent tutorial on how to do it, check the articles here on gamedev.net as well.

zipless

##### Share on other sites
Kwizatz    1392
The best tool you can find is Numega Bounds Checker, but its expensive (it''s only downside to me is the price), then there is memprof in the Linux world, dont know if someone has already ported that to Windows.

##### Share on other sites
Oluseyi    2116
quote:
Original post by zipless
The best way i''ve found is to overload the new&delete or alloc and free functions.

Does that, in your estimation, surpass the CRT family of debug functions declared in <crtdbg.h>?

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

##### Share on other sites
Kylotan    10013
I seem to remember being able to get the MS CRT functions to tell me when I had memory leaks, but not how I''d allocated those leaks. Therefore I couldn''t find how to fix it. I looked through the docs but didn''t have any luck. Any clues on how to use these functions more effectively?

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

##### Share on other sites
S1CA    1418
Alternatively you can use a package such as nuMega BoundsChecker which will do extensive leak detection, even on things like GDI resource handles etc.

Kylotan:

http://www.gamedev.net/community/forums/topic.asp?topic_id=71992

--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com

##### Share on other sites
zipless    122
I''ve never used crtdbg so i wouldn''t know. I like my overload functions, i can see exactly how much memory i''m using at any time and it has the nice leak detection thing as well.

zipless

##### Share on other sites
Shannon Barber    1681

##### Share on other sites
Kylotan    10013
Thanks Simon. There is a #define mentioned in MSDN which claims to insert the file/line information, but it simply doesn''t.

##### Share on other sites
LordElectro    122
BoundsChecker, all the way.

##### Share on other sites
Kylotan    10013
Actually Simon, that code in the other thread gives me numerous compile-time errors when I try it. The source tags mangled your post in that thread however, so I might have entered it incorrectly or missed something. Any chance you could repost the exact syntax of what comes after the ''#define _CRTDBG_MAP_ALLOC'' line (which MSDN implies should be enough to show the file/line info on its own, but doesn''t)?

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

##### Share on other sites
S1CA    1418
Kylotan:

Compiles fine for me as posted (MSVC 6) - I just created a new Win32 application, copied in the code from the post, sorted out the newlines and hit F7 - all fine.

What compile errors are you getting ?

Including the crtdbg.h header is essential, as are the 2 #defines which replace the "new" operator with a macro which calls the special CRT debugging version of the new operator.

All #''s need to start on a new line

The _CrtSetDbgFlag calls are necessary to enable the reports at program exit.

BTW: The reason just enabling the _CRTDBG_MAP_ALLOC doesn''t work is due to an oversight at MS - that the __LINE__ and __FILE__ are converted to strings by the preprocessor rather than the compiler! This means that the __LINE__ and __FILE__ in crtdbg only give you the line number in that file - doing the overloading/overriding in a macro means that the __LINE__ and __FILE__ are resolved by the preprocessor on a per use basis.

--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com

##### Share on other sites
Mezz    571
Dare I mention Midnight''s memory manager?

Pretty good, if I remember rightly.

-Mezz

##### Share on other sites
Kylotan    10013
This is the code I have in the first header file included by my .cpp file:
#ifdef _DEBUG#define _CRTDBG_MAP_ALLOC#include "crtdbg.h"#define DEBUG_NEW_FN new( _NORMAL_BLOCK, __FILE__, __LINE__)#define new DEBUG_NEW_FN

Which is pretty much copy/pasted from the other thread. I get these errors:
stdlib.h(281) : error C2059: syntax error : 'constant'
stdlib.h(281) : error C2733: second C linkage of overloaded function '_calloc_dbg' not allowed
stdlib.h(281) : see declaration of '_calloc_dbg'
stdlib.h(283) : error C2059: syntax error : 'constant'
stdlib.h(283) : error C2733: second C linkage of overloaded function '_free_dbg' not allowed
stdlib.h(283) : see declaration of '_free_dbg'
stdlib.h(298) : error C2059: syntax error : 'constant'
stdlib.h(298) : error C2733: second C linkage of overloaded function '_malloc_dbg' not allowed
stdlib.h(298) : see declaration of '_malloc_dbg'

etc etc etc...

Looks like it's getting done twice... but the code I posted above is the first thing I #include, so it should affect everything... right?

The code in the article Magmai linked to works fine, by the way: just a slightly different syntax.

I'm using MSVC6.

PS. Don't worry too much about this: it occured to me that I would need to add this individually to every single file in my project, which is pretty unviable at the moment. Why is there no equivalent to __file__ which evaluates to the file you're compiling, rather than the file you actually typed it into? *sighs*

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Edited by - Kylotan on January 19, 2002 8:13:50 PM

Edited by - Kylotan on January 19, 2002 8:21:12 PM

##### Share on other sites
S1CA    1418
Dunno...

always works for me - no matter what abuse I try to put it through. Also ok worked for the other 3 programmers on our current project.

Have you tried it in a totally fresh, project workspace rather than an existing project ?

Also since that particular example I posted was intended to all be in a .cpp file, you should probably add an inclusion guard to the header file.

Other things it could be:

- other code, elsewhere doing the same thing.

- other code with a different memory manager clashing with the CRT one.

- overloaded operator new somewhere which doesn''t support the extra parameters.

--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com

##### Share on other sites
Kylotan    10013
I think I found it - certain STL files include the "new" header which contains the following:
		// new AND delete DECLARATIONSvoid __cdecl operator delete(void *) _THROW0();void *__cdecl operator new(size_t) _THROW1(std::bad_alloc);void *__cdecl operator new(size_t, const std::nothrow_t&)	_THROW0();
along with another overloaded new elsewhere. Maybe they are conflicting.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

##### Share on other sites
daerid    354
quote:
Original post by Magmai Kai Holmlor
Article on finding Memory Leaks

Hate to bump an old thread, but I found that link EXTREMELY useful.

##### Share on other sites
Dark Rain    157
I read the article, I implemented the class and started it in my main class, started the program in debug mode to see the results.

I have results as the page said BUT they only report to the Header of the debug class. I cant find where the memory leaks are that way.

Is it because I need to insert the debug class header in all my classes or some such?

##### Share on other sites
Pure Krome    122
Also ...

what program is good for finding what is leaking under LINUX?

and in C++ also.

-PK-

##### Share on other sites
xDS4Lx    126
Hmm, I havent used these in a while but I found them like a year, maybe a year and a half ago on flipcode, but I cant find the link to them anymore so here: htt://pigseye.kenesaw.edu/~ncynkus/MMGR.zip

Its a Memory management header and cpp file you include in your program and it gives you a report, credit to: Paul Nettle

##### Share on other sites
Kylotan    10013

The FindMemoryLeaks class at the site Magmai pointed out (http://www.kbcafe.com/articles/memory.leaks.html) appeared to work well for several of my applications, but just recently I found it was reporting memory leaks in my app which I was sure was watertight. After doing a bit more investigation, it appeared that it was reporting memory leaks of memory that I never allocated, which I can only assume was allocated by the C runtime library or the C++ standard library, even though they weren''t marked as ''CRT'' memory blocks. This is despite me using the FindMemoryLeaks class as the only static object in my entire program (everything else was a member of or created by the Application class, which was a local to main().) So I took out that class, and replaced it with this in my main.cpp file:
#include <crtdbg.h>#ifdef _DEBUG#define _CRTDBG_MAP_ALLOC#define new new(_NORMAL_BLOCK,__FILE__, __LINE__)#endif...int main(int, char**){    _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );    ... rest of program

This now proceeded to only report a single memory leak, which was the one I deliberately put into my programs to check that my leak-detection is working

Now, although it might seem arrogant to believe the checking routine that made my code look better, I was sure in this case, after having commented 99% of the code out, that these leaks were something to do with the standard library, and that _CrtSetDbgFlag somehow accounts for this. (The other alternative is that the FindMemoryLeaks class was itself leaking memory, which shouldn''t happen as it allocates nothing.)

So, in summary, I recommend using the _CrtSetDbgFlag function rather than the LeakChecker class. I may be doing something wrong, but it''s usually best to prefer the tool that makes it harder to do something wrong

Oh, and Dark Rain, you need the #define new line in the actual CPP file, as the __FILE__ and __LINE__ macros are resolved relative to the physical file you type them into. It''s tedious to do, but worth it in the end if a correct program is what you''re after.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]