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

Started by
15 comments, last by johnnyBravo 19 years, 10 months ago
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).

---------------------------Hello, and Welcome to some arbitrary temporal location in the space-time continuum.

Advertisement
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]
The method I use is actually outlined quite nicely over on flipcode - you should check it out.

---------------------------Hello, and Welcome to some arbitrary temporal location in the space-time continuum.


Looks like this is it: How to find memory leaks

Thanks for pointing out that article Etnu


[edited by - EvilSteve on May 28, 2004 2:13:43 PM]
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...

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.
Paul Nettle''s memory manager is very, very good, and very easy to use.

This topic is closed to new replies.

Advertisement