How-To‎ > ‎

Memory Leak Detection




The below list of options are simplest ways to detect memory leak in c\c++ code.

 

1.  A Cross-Platform Memory Leak Detector

 

Audience: Cross Platform developers who want to detect memory leaks in their applications. Detects leak by only new.

 

How-To Details: http://wyw.dcweb.cn/leakage.htm

 

Source Code: http://sourceforge.net/projects/nvwa/files/latest/download?source=files

 

References:

http://ntcoder.com/bab/2008/08/13/debug_new-explained/

This does not detect leak by malloc or calloc.

 

 

Hands-On:

 

Code:

#include <stdlib.h>

 

#include "debug_new.h"

  

int main(int argc, char *argv[]) {

    int *var = new int(5);

 

    char *tmp = (char *) malloc(5);

    char *test = (char *) calloc(8,sizeof(char));

 

    return 0;

}

 

 

Compile:

g++ testmemleak.cpp debug_new.cpp -o test

 

 

Run:

./test

Leaked object at 0xf3a0060 (size 4, testmemleak.cpp:6)

*** 1 leaks found

 

 

 

2.  Detecting Memory Leaks using the CrtDbg Library

 

Audience: Win32/Win64 developers who want to detect memory leaks in their applications. Detects leak by both new and malloc\calloc.

 

How-To Details: http://www.codeproject.com/Articles/66324/Detecting-Memory-Leaks-using-the-CrtDbg-Library

 

GCC memory leak detection equivalent to Microsoft crtdbg.h?

After many years of working on a general-purpose C++ library using the Microsoft MSVC compiler in Visual Studio, we are now porting it to Linux/Mac OS X (pray for us). I have become accustomed and quite fond of the simple memory leak detection mechanism in MSVC:

 

Other References:

http://sourceforge.net/projects/crtdbg4wince/

http://ideone.com/oECgh

http://stackoverflow.com/questions/8718758/using-crtdumpmemoryleaks-to-display-data-to-console

http://social.msdn.microsoft.com/Forums/vstudio/en-US/86830f7e-ec90-4f3c-aa1d-6e31ba339eae/debugnew-only-for-mfc

http://stackoverflow.com/questions/2153776/how-to-use-crtdumpmemoryleaks

Comments