Archived

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

How to detect memory leaks???(c++)

This topic is 4944 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

Hi, ive just wrapped up directsound8 classes(c++), and im because of the large amount of code im afraid i may of left memory leaks. Im wondering how would i test if there is any leaks going on? Thanks, [edited by - johnnyBravo on May 27, 2004 8:55:41 PM]

Share this post


Link to post
Share on other sites
There are a couple of ways to deal with this:

1. Customize your memory handling routines and verify that all your resources are being released correctly. I think the Game Gems series has a few articles on this.

2. Bounds-checker.

3. Purify.

I personally like purify but this is not an inexpensive tool. #1 may be your best bet for a low budget solution. You could also incorporate some advanced memory handling techniques to deal with the fragmentation issues that are prevalent in handling the thousands of resources you need to keep resident.


#dth-0

Share this post


Link to post
Share on other sites
#if defined (_DEBUG)
CMemoryState msOld,msNew,msDiff;
#endif

void main(){

#if defined (_DEBUG)
msOld.Checkpoint();
#endif

//program code here

#if defined (_DEBUG)
msNew.Checkpoint();
if(msDiff.Difference(msOld,msNew)){
TRACE("Memory leaked!\n");
msDiff.DumpStatistics();
}
#endif

}

[edited by - wavarian on May 27, 2004 12:14:32 AM]

Share this post


Link to post
Share on other sites
thanks all, i ended up using alnight''s

#define CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
//

_CrtDumpMemoryLeaks();


and to my amazement, after remembering to put in the delete on the pointers, works perfectly, no memory leaks from my wrapper, which i dont know how its possible

Share this post


Link to post
Share on other sites

Personally, I overload the new and delete operators, have new increment a counter everytime memory is allocated, have delete decrement a counter when memory is deallocated, and then check that my counter == 0 at the end of the program. This seems to be more cross-platform than using Microsoft''s debugging stuff, if cross-platform is something that you''re looking for...

Share this post


Link to post
Share on other sites
don''t know if this will work. its from the gamedev newsletter:


===================
5. Tip of the Week
-------------------
Simple Memory Leak Detection in Visual Studio
By: Drew "Gaiiden" Sikora
=============================================

I stumbled across this in my wanderings last night. All credit, of course,
goes out to wheoever first came up with this, which wasn''t me. According
to Bad Maniac over at http://2dnow.zenzer.net/index.php, it was brought to

his attention by Ethereal Darkness of http://etherealdarkness.com/ and now

I bring it to you, because it rocks.

#include "crtdbg.h"
void DetectMemoryLeaks()
{
_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
_CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
}

Now just call this function at the start of your program and if you''re
compiling in debug mode (F5), any leaks will be displayed in the Output
window when the program shuts down. If you''re not in debug mode this will
be ignored. Use it as you will!

Share this post


Link to post
Share on other sites
I just override new and delete (while in debug), and keep track of memory allocations in a linked list. I also make sure that new sends me the line # and source file, which is dumped at program shut down in the event of a leak. That has made sure I never get leaks, without putting any overhead on in release mode.

(to prevent recursion in the linked list, I always use malloc() and free() for list allocation).

Share this post


Link to post
Share on other sites
Overriding the new and delete operators is actually very simple. Here is an example (mostly from memory, don't have the code on me right now):


#if defined(_DEBUG)

#include <malloc.h>

static unsigned int gMemoryAllocations = 0;

void* operator new(size_t xNumBytes)
{
gMemoryAllocations++;
malloc(xNumBytes);
}

void operator delete(void* xpMemoryBlock)
{
gMemoryAllocations--;
free(xpMemoryBlock);
}

void CheckMemoryLeaks()
{
if (gMemoryAllocations > 0)
{
// cout, cerr, printf, fprintf, MsgBox, whatever you want

// gMemoryAllocations + " memory leaks detected"

}
}

#else // defined(_DEBUG)


void CheckMemoryLeaks()
{
}

#endif // defined(_DEBUG)



As Etnu pointed out, you are able to make this memory leak checking scheme as versatile as you need it to be. By defining the new operator as a macro, you can report on which lines of your source code the memory leaks have taken place.


[edited by - EvilSteve on May 28, 2004 1:22:51 PM]

[edited by - EvilSteve on May 28, 2004 6:46:15 PM]

Share this post


Link to post
Share on other sites
Hi,

I am VERY new at searching for memory leaks and have just tried a couple of the methods mentioned in the above posts. The method from the Gamedev newsletter posted by TheOne1. My project is made up of three .cpp''s and three headers. I put the code not in a class but right at the top of the .cpp file with the WinMain() function in. I then called the function from the WinMain() function right at the very beginning. Is this the correct way or have I mis-read the instructions because I get no message in the debug window once I have F5''ed the app and exited from it. Or does this simply mean the codes clean.. just a bit dubious about having such clean code.

I also had a go with Alnite''s putting the code right at the top of the file with the WinMain() function in. No memory leak messages there either.

These must not be working correctly because my code is never that clean...

Share this post


Link to post
Share on other sites
Having a few problems with the MS version, just wondering if anyone had got that working?

In my main .cpp which holds the WinMain() right at the top of the file I have:-

#define CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

Then at the start of WinMain() I have,

_CrtDumpMemoryLeaks();

I get nothing.

However I get some joy from:-

void DetectMemoryLeaks(){ _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF); _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE); _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);}

I then call this func in WinMain() but it just gives the basic:-

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


Even if I declare #define CRTDBG_MAP_ALLOC. What I want to get is the full version so I can click on the program name and have it jump straight to the problem line, like the MS website mentions:-

Detected memory leaks!
Dumping objects ->
C:\PROGRAM FILES\VISUAL STUDIO\MyProjects\leaktest\leaktest.cpp(20) : {18}
normal block at 0x00780E80, 64 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.

Any help appreciated, sorry if this post is crappy, long night and brain tired.

Share this post


Link to post
Share on other sites