Skip to main content
GameDev.net gamedev.net
🔒 Locked

memory leaks

Started by russian-bear Apr 21, 2002 at 5:39 PM 6 replies 250+ views
Original Post
russian-bear
russian-bear
Hi! Every one is talkin about memory leaks if your not careful with memory alocaiton and deallocation. So i have always wondered how can you notice if your game leaks memory?
< There are no stupid questions, only stupid answers! >
intransigent-seal
intransigent-seal
The computer will slow down as the program runs. And if it''s a serious leak, then the computer may stay slow after the application has finished.
But if you want to be more high-tech about it, then I expect there are tools to monitor and log memory levels which would help you see if there is a leak.
As for finding the source of the leak - that''s harder, sometimes there are clues - but it depends on the specific case.

John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
russian-bear
russian-bear
OK thanks!
< There are no stupid questions, only stupid answers! >
DC_Tsunami
DC_Tsunami
On some operating systems, the program will actually crash on exit if it doesn''t release all of its memory. I don''t know if it''s supposed to do this, but it does on my system (winME). It is useful in a way because it lets you know when you have a leak.

If you want to detect leaks from within your program, there are ways to do it in C++ I believe, by overloading the new and delete operators.
Anthracks
Anthracks
If you want to detect memory leaks (info shamelessly stolen from this flipCode thread) using MSVC, you can do something like this. First, include this either at the top of the files you want to check for leaks, or in another header file that is included in everything.


    
#ifdef _DEBUG

#include <crtdbg.h>

#define new new(_NORMAL_BLOCK,__FILE__, __LINE__)

#endif


And then in your main or WinMain function, put this:


#ifndef NDEBUG

int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); // Get current flag


flag |= _CRTDBG_LEAK_CHECK_DF; // Turn on leak-checking bit


_CrtSetDbgFlag(flag); // Set flag to the new value


#endif


Then just compile in debug mode and start debugging, and when the program ends, it will report every memory leak along with the line where the memory was allocated, which should give you a hint where to look. Good luck .

Anthracks

edit: formatting.

[edited by - Anthracks on April 21, 2002 7:39:44 PM]
Kylotan
Kylotan
quote:
Original post by Anthracks
First, include this either at the top of the files you want to check for leaks, or in another header file that is included in everything.

Unfortunately, it doesn''t work that way, as it just ends up reporting the name of the header file as where the memory was allocated. You need to explicitly put it in every .CPP file. *sigh*

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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.