Advice on avoiding memory leaks

Started by
7 comments, last by MessageBox 20 years, 6 months ago
I just thougth I''d pass on some handy advice for avoiding memory leaks using _CrtDumpAllMemoryLeaks(); Use this as the very last function in your program before you return to windows and if it will tell you if you are leaking memory, simple as that....... I personally was pissing away an array of 256 floats every single time I ran my engine because I was accidentally allocating a FONT class twice. I''m too scared to go back and try it in some of my early demoes, God only knows how much memory I was leaking. If you''re using MFC, the CMemoryState class provides the same debugging info.
Advertisement
Is that a VC++ only function? Or is it from a certain include (stdio?).
[www.LifeIsDigital.net - My open source projects and articles.
Apparently this is VC:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvc60/html/memleaks.asp

You need the following:

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

Interesting. Thanks for bringing this to my attention.

My personal anti-leak methods:
1. Don''t allocate memory. Stop the problem at it''s source!
2. Any database that doesn''t fit on the stack is not worth keeping around.
3. Call delete everything; before exiting. This involves defining it to be: char *everything = "All my program''s variables";


Tom
"E-mail is for geeks and pedophiles." -Cruel Intentions
Hey.

I have never known about this before
Thanks a lot!
It''s not easy to find memory leaks, so this is a great help to me :D

"Most people would sooner die than think; in fact, they do so. "
Wow, nice.
Just use a memory debugger library, like Electric Fence. :D You''ll get yourself a nice lil'' core dump every time it detects a leak. A very efficient way to find ''em.
quote:Original post by ParadigmShift

My personal anti-leak methods:
1. Don''t allocate memory. Stop the problem at it''s source!
2. Any database that doesn''t fit on the stack is not worth keeping around.
3. Call delete everything; before exiting. This involves defining it to be: char *everything = "All my program''s variables";


Tom


LOL, have to memorize these three rules
A great resource that everyone should look at at least once is the flipcode.com Tip of the Day gallery (and the Code of the Day gallery). Several tips from the last couple of years are archived there, including memory leak detection tips. In fact, this tip is related to Debug mode memory leak checking in VC++, with a nice spew in the debug output window including file names and line numbers.

Surf around the site a bit (look for the Ask Midnight column) and you''ll find Paul Nettle''s memory manager, which as I recall is not tailored to a specific compiler. It predates the one presented in Game Programming Gems 2 (which is VC-specific and list Paul''s code as a reference). Simple, and very useful for large projects.
Actually, although I originally suggested using _CrtDumpMemoryLeaks() a better way to check for leaks is as follows:

#define _CRTDBG_MAP_ALLOC// At the start of the program call_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );// Could be anywhere though, just easier to remember to do it at the start)


_CrtDumpMemoryLeaks() can sometimes show memory leaks even though there are none,
ie. if you''re using the std::cout, etc.. it''ll show these objects as leaked memory when the programs ends.

This topic is closed to new replies.

Advertisement