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

Testing a program for memory leaks

Started by Drazgal Nov 26, 2001 at 5:11 PM 7 replies 500+ views
Original Post
Drazgal
Drazgal
I''ve been building a class set for a linked list, now I know theres the standard STL linked list but I wanted to try my own, the problem is although Im pretty sure it deletes all the nodes correctly I have no real way of knowing, plus I want to be able to test my game for memory leaks, Im not very familiar with my debugger (Visual C++ 6.0) and I was wondering if anyone could tell me how I can check for memory leaks? Thanks! Ballistic Programs
Fruny
Fruny
There is a (not free, and not cheap either) plugin called Purify that does that. With gcc you can use mcheck().

Otherwise, with VC++, I''m afraid the only ''easy'' solution is to fire up the task manager and to wait... and see if memory usage goes up.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Stoffel
Stoffel
It''s possible to do that without MFC (_CRT something or another). I know for sure that Flipcode has had discussions on this. We have bounds checker here so I''ve never had to worry about it.
Kylotan
Kylotan
Put code in the constructors and destructors to count/log what gets created and destroyed. Send it to cout or a log file or something.
Diodor
Diodor
Search for ''bugslayer'' in the MSDN, there are some good articles there about the crt_ debug functions.
outRider
outRider
This was posted on FlipCode a while back, so it's not mine.

#include crtdbg.h

// Memory leak detection in debug build
#ifndef NDEBUG
int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
flag |= _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag(flag);
#endif

If you run your app in the debugging environment (Go command, F5) it will tell you if you have any leaks in the debug output. If you don't get anything then it probably means you don't have a leak, so make a 100k leak or something to see if it works for you.

------------
- outRider -

Edited by - outRider on November 26, 2001 12:27:01 AM
S1CA
S1CA
Assuming you''re using new and delete


1. create a header file in your project called "debug_new.h", it''s contents should be as follows:
  
#ifndef DEBUG_NEW_H
#define DEBUG_NEW_H

#ifdef _DEBUG

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

#define DEBUG_NEW_FN new( _NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW_FN

#else

#define DEBUG_NEW_FN

#endif//_DEBUG


#endif//DEBUG_NEW_H




2. In the file with the main entry point of your program (WinMain(), DllMain() or main()), #include "crtdbg.h".


3. In the same file, at the first place your code enters, copy the code that outRider posted. Alternatively put a "_CrtDumpMemoryLeaks();" on every program exit point.


4. In *every* file which does some form of memory allocation which you want to check for leaks, #include "debug_new.h"


Points 2 and 3 on their own WILL tell you that you have memory leaks, but will give you a tracking ID. You then have to mess around setting a quickwatch on "_crtBreakAlloc" for each tracking ID you have [a PITA].

Points 1 and 4 fix a small bug/misunderstanding in the CRT debug code. When the bug is fixed (1 & 4), instead of just getting an allocation ID, you get the correct source file and line number of where the allocation was made. You can simply double click on the report to go straight to the line in the IDE!

In summary:

with 2 & 3 (and what outRider posted) you get output like:

Detected memory leaks!
Dumping objects ->
{19} normal block at 0x007806E0, 4 bytes long.
Data: < > CD CD CD CD
{18} normal block at 0x00780710, 64 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.


By adding 1 & 4 you get output like:

Detected memory leaks!
Dumping objects ->
c:\_PROJECTS\memleak\main.cpp(19) : {19} normal block at 0x007806E0, 4 bytes long.
Data: < > CD CD CD CD
c:\_PROJECTS\memleak\main.cpp(18) : {18} normal block at 0x00780710, 64 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.


--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com
Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site
Sturm
Sturm
You can also visit flipcode at look at this tutorial:

How To Find Memory Leaks

It shows how to create a memleak detector. The thing here is that you can easily extend it and it's portable (more or less), as opposed to DEBUG_NEW which only works with VS (As far as I know).

I've had some good experiences with it, but it's basic and you might have to modify it to suit you, but hey isn't all code like that?

Well that's that.

-- Sturm

Edited by - Sturm on November 27, 2001 8:19:13 AM
---------------------------------------------------Life after death? No thanks, I want to live NOW --- Sturm 2001

Topic Locked

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

Sign in to reply to this topic.