Game Development Reference
In-Depth Information
tricky bug will be easier to catch. The second flag tells the debug heap that you want
to run a complete check on the debug heap ' s integrity each time memory is allocated
or freed. This can be incredibly slow, so turn it on and off only when you are sure it
will do you some good.
The output of the memory leak dump looks like this:
Detected memory leaks!
Dumping objects ->
c:\tricks\tricks.cpp(78) : {42} normal block at 0x00321100, 100 bytes long.
Data: <
> CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.
The program '[2940] Tricks.exe: Native' has exited with code 0 (0x0).
As you can see, the leak dump pinpoints the exact file and line of the leaked bits.
What happens if you have a core system that allocates memory like crazy, such as a
custom string class? Every leaked block of memory will look like it
'
s coming from the
same line of code, because it is. It doesn
t tell you anything about who called it, which
is the real perpetrator of the leak. If this is happening to you, tweak the redeclaration
of new and store a self-incrementing counter instead of __LINE__ :
'
#include <crtdbg.h>
#if defined _DEBUG
static int counter = 0;
#define GCC_NEW new(_NORMAL_BLOCK,__FILE__, counter++)
#endif
The memory dump report will tell you exactly when the leaky bits were allocated,
and you can track the leak down easily. All you have to do is put a conditional break-
point on GCC_NEW and break when the counter reaches the value that leaked.
The Task Manger Lies About Memory
You can
t look at the Task Manager under Windows to determine if your game
is leaking memory. The Task Manager is the process window you can show if
you press Ctrl-Alt-Del and then click the Task Manager button. This window
lies. For one thing, memory might be reported wrong if you have set the
_CRTDBG_DELAY_FREE_MEM_DF flag. Even if you are running a release
build, freed memory isn
'
t reflected in the process window until the window is
minimized and restored. Even the Microsoft test lab was stymied by this one.
They wrote a bug telling us that our game was leaking memory like crazy, and
we couldn
'
t find it. It turned out that if you minimize the application window
and restore it, the Task Manager will report the memory correctly, at least for a
little while.
'
Search WWH ::




Custom Search