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

Finding memory leaks

Started by Arek the Absolute Mar 11, 2004 at 11:30 PM 9 replies 2k views
Original Post
Arek the Absolute
Arek the Absolute
I've found a memory leak in my current project, and although it's easy enough for me to repeat it and find the user input that causes it, I can't seem to track down where in code it occurs. I'm currently using VC++ 6.0 with _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF); _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE); _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR); to print out any memory leaks it finds, and although it shows its output fine: Detected memory leaks! Dumping objects -> {4423} normal block at 0x004544F0, 5 bytes long.  Data: <room > 72 6F 6F 6D 00 {4422} normal block at 0x00454670, 5 bytes long.  Data: <room > 72 6F 6F 6D 00 {4421} normal block at 0x004546B0, 5 bytes long.  Data: <room > 72 6F 6F 6D 00 {4420} normal block at 0x00454770, 5 bytes long.  Data: <room > 72 6F 6F 6D 00 Object dump complete. I can't seem to find where the memory is allocated to begin with, much less why it is never deallocated. Unfortunately the data, "room " doesn't help very much in this particular situation. However, I have noticed that the address of the leaked data is pretty consistent. This has made me wonder if there's a way I can set a debugger to break when a certain memory location is accessed, so I can at least know where the memory is allocated, and then figure out where it should be deallocated and why it isn't. I've been searching for a rather while and will continue to do so, but I've been wondering if there is an easier way to go about hunting down this memory leak. Thanks in advance for any advice you may have! -Arek the Absolute [edited by - Arek the Absolute on March 11, 2004 12:31:29 AM]
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig
Rayno
Rayno
Hello. Indeed there is a way to break when the memory is allocated. You can read how right here.
antareus
antareus
After your other CRT debug settings:
_CrtSetBreakAlloc(4420);

The 4420 I grabbed from between the braces in the debug output.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Jaco vd Westhuizen
Jaco vd Westhuizen
Or go to

http://www.fluidstudios.com/pub/FluidStudios/MemoryManagers/Fluid_Studios_Memory_Manager.zip

and download their memory manager.

It tames some effort to integrate it into your application, but I'll tell ya, it's pretty darn good!


This sucker will tell you exactly where your memory leak is.

[edited by - Jaco vd Westhuizen on March 12, 2004 1:05:30 AM]
<$1,000,000 signature goes here>
Neophyte
Neophyte
Insert the following at the top of your main-file:

#ifdef _DEBUG
#define new new(_NORMAL_BLOCK,__FILE__, __LINE__)
#endif


And the following at the start of your main (or WinMain) method:

#ifndef NDEBUG
int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
flag |= _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag(flag);
#endif


Although you probably already did the last part, so only redefining new is likely to be necessary.

This should tell at you at which line in which file the memory that leaks was allocated.

Hope that helps,
- Neophyte
Arek the Absolute
Arek the Absolute
Thanks all of you. It was Neophyte''s idea that I ended up using to solve the problem, but all of the suggestions were useful. Thanks a lot!

-Arek the Absolute
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig
scratch
scratch
Neophyte,

Where is _NORMAL_BLOCK defined? My code doesn''t compile when I redefine ''new'' as you suggested.
SiCrane
SiCrane
It should be in crtdbg.h.
caesar4
caesar4
the fluid studios memory leak tracer would be the last resort when doing time critical code
it runs slow as hell, so you would be better off writing your own manager, compile it into a separate dll so if your prog crashes, manager would still work



Cartman''s definition of sexual harrasement:
"When you are trying to have intercourse with a lady friend, and some other guy comes up and tickles your balls from behind"

(watch South Park, it rocks)
Cartman's definition of sexual harrasement:"When you are trying to have intercourse with a lady friend, and some other guy comes up and tickles your balls from behind"(watch South Park, it rocks)
Rickmeister
Rickmeister
You could overload the new and delete operator. Look at www.flipcode.com for a tutorial explaning how to implement it.. Quite useful. gives you file output where you allocated the mem..

Topic Locked

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

Sign in to reply to this topic.