Jump to content
  • Advertisement

Archived

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

MessageBox

Advice on avoiding memory leaks

This topic is 5384 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!