Click here to Skip to main content
15,892,927 members
Articles / Programming Languages / C++

Memory Leak Finder

Rate me:
Please Sign up or sign in to vote.
3.68/5 (39 votes)
22 Feb 2012MIT2 min read 346K   4.5K   126   90
Custom memory handler with memory leak reporting and no-mans-land checking. Leaks are reported with call stack of allocation.

Sample Image - Memory_leak_finder.jpg

Introduction

Have you ever had a memory leak? Wished you knew where you allocated it and how? Is your boss cheap and refuses to buy Boundchecker or another debugging tool?

Here is the solution for you. A memory leak detector compiled directly into your code. It reports memory leaks with call stack of the allocation down to a user defined depth.

As an add-on, it does simple checks of the memory before and after the memory block, to track buffer overwrites.

Usage

Include tracealloc.cpp in your project. Define DETECT_LEAKS in the project settings. Compile. Run your application. Memory leaks are reported into your debug output window when the application terminates. Just click the memory leak and the correct file and line will be shown.

You can find further instructions in the source code.

How Is It Done?

The code overrides operator new and operator delete. For each allocation made, the code allocates extra space for tracking, call stack and no-mans-land. The current call stack is fetched and remembered, finally the code puts the newly allocated block in a linked list and the requested memory is returned.

When a memory block is deleted, the header is found and checked for buffer overwrites. The memory block is then removed from the linked list and deallocated.

When the program terminates, the global memory tracker object is deleted. The destructor traverses the linked list for memory blocks that isn’t deleted (= leaked memory). It then fetches symbol information for the call stacks and dumps the information in the debug console.

Limitations

The code is Microsoft Visual Studio and Win32 specific. It requires a debug build. The code is C++ specific. It handles new/delete but not malloc/free. The code will run slower with leak detection active (roughly at half normal debug build speed).

Finally

I want to thank Zoltan Csizmadia who wrote ExtendedTrace. I have used parts of his code for stack walking and symbol lookups.

I also want to thank the Code Project community. I have found many solutions or pointers in the right direction here. I hope I have given something back with this contribution.

Thank you!

History

  • February 22, 2012
    • Inclusion of contributed bug fixes (author acknowledged in read me file)
    • Upgrade of solution to Visual Studio 2010

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer (Senior)
Sweden Sweden
B.Sc in Software engineering

Writing software for the finance market.
Languages known: C/C++, SQL, Java, Perl, M68000 assembly and more. Give me the syntax and I'll program in it.

In my spare time i like to watch movies, read books and play computergames.

Comments and Discussions

 
GeneralRe: No line number information Pin
Member 3374401-Apr-05 3:07
Member 3374401-Apr-05 3:07 
GeneralRe: No line number information Pin
gualo27-Apr-05 3:19
gualo27-Apr-05 3:19 
GeneralRe: No line number information Pin
gualo27-Apr-05 3:58
gualo27-Apr-05 3:58 
GeneralRe: No line number information Pin
stamari29-Jan-07 5:56
stamari29-Jan-07 5:56 
GeneralRe: No line number information Pin
mcanti3-Feb-07 22:41
mcanti3-Feb-07 22:41 
GeneralRe: No line number information Pin
BeakerBob5-Nov-07 6:00
BeakerBob5-Nov-07 6:00 
GeneralRe: No line number information Pin
mark_xlprint9-May-05 21:51
mark_xlprint9-May-05 21:51 
GeneralWTL project error Pin
Killerluke4-Mar-04 6:27
Killerluke4-Mar-04 6:27 
Hello
I've try to use your Memory Leak Finder in a WTL project, but the VC7 compiler show me this error:

c:\Programmi\Microsoft Visual Studio .NET 2003\Vc7\include\xdebug(29) : error C2833: "operator DEBUG_NEW" non è un operatore o un tipo riconosciuto
c:\Programmi\Microsoft Visual Studio .NET 2003\Vc7\include\xdebug(29) : error C2059: errore di sintassi: "newline"
c:\Programmi\Microsoft Visual Studio .NET 2003\Vc7\include\xdebug(33) : error C2833: "operator DEBUG_NEW" non è un operatore o un tipo riconosciuto
c:\Programmi\Microsoft Visual Studio .NET 2003\Vc7\include\xdebug(33) : error C2059: errore di sintassi: "newline"
c:\Programmi\Microsoft Visual Studio .NET 2003\Vc7\include\xlocale(479) : error C3861: "DEBUG_NEW": identificatore non trovato nemmeno con una ricerca dipendente dall'argomento
c:\Programmi\Microsoft Visual Studio .NET 2003\Vc7\include\xlocale(479) : error C2146: errore di sintassi: ";" mancante prima dell'identificatore "wchar_t"
c:\Programmi\Microsoft Visual Studio .NET 2003\Vc7\include\xlocale(479) : error C2275: "wchar_t": tipo non valido come espressione
c:\Programmi\Microsoft Visual Studio .NET 2003\Vc7\include\stdlib.h(78): vedere la dichiarazione di "wchar_t"
d:\WTL_INCLUDE\KWL\Misc\LeakWatcher.h(13) : error C2833: "operator DEBUG_NEW" non è un operatore o un tipo riconosciuto
d:\WTL_INCLUDE\KWL\Misc\LeakWatcher.h(13) : error C2059: errore di sintassi: "newline"
d:\WTL_INCLUDE\KWL\Misc\LeakWatcher.h(14) : error C2143: errore di sintassi: ";" mancante prima di "{"
d:\WTL_INCLUDE\KWL\Misc\LeakWatcher.h(14) : error C2447: "{": intestazione di funzione mancante. Elenco formale di tipo obsoleto?
d:\WTL_INCLUDE\KWL\Misc\tracealloc.cpp(643) : error C2365: "new": ridefinizione. La definizione precedente era "funzione membro"
d:\WTL_INCLUDE\KWL\Misc\tracealloc.cpp(643) : error C2078: troppi inizializzatori
d:\WTL_INCLUDE\KWL\Misc\tracealloc.cpp(643) : error C2440: "inizializzazione": impossibile convertire da "int" a "void *"
La conversione da tipo integrale a tipo puntatore richiede reinterpret_cast, cast di tipo C o cast di tipo funzione
d:\WTL_INCLUDE\KWL\Misc\tracealloc.cpp(643) : error C2143: errore di sintassi: ";" mancante prima di "("
d:\WTL_INCLUDE\KWL\Misc\tracealloc.cpp(643) : error C2226: errore di sintassi: tipo "size_t" imprevisto
d:\WTL_INCLUDE\KWL\Misc\tracealloc.cpp(643) : error C2059: errore di sintassi: ")"
d:\WTL_INCLUDE\KWL\Misc\tracealloc.cpp(644) : error C2143: errore di sintassi: ";" mancante prima di "{"
d:\WTL_INCLUDE\KWL\Misc\tracealloc.cpp(644) : error C2447: "{": intestazione di funzione mancante. Elenco formale di tipo obsoleto?
d:\WTL_INCLUDE\KWL\Misc\tracealloc.cpp(648) : error C2365: "new": ridefinizione. La definizione precedente era "funzione membro"
d:\WTL_INCLUDE\KWL\Misc\tracealloc.cpp(648) : error C2078: troppi inizializzatori
d:\WTL_INCLUDE\KWL\Misc\tracealloc.cpp(648) : error C2440: "inizializzazione": impossibile convertire da "int" a "void *"
La conversione da tipo integrale a tipo puntatore richiede reinterpret_cast, cast di tipo C o cast di tipo funzione
d:\WTL_INCLUDE\KWL\Misc\tracealloc.cpp(648) : error C2143: errore di sintassi: ";" mancante prima di "["
d:\WTL_INCLUDE\KWL\Misc\tracealloc.cpp(648) : error C3409: blocco attributi vuoto non consentito
d:\WTL_INCLUDE\KWL\Misc\tracealloc.cpp(649) : error C2470: "s": sembra una definizione di funzione, ma manca un elenco di parametri formali. Il corpo apparente della funzione verrà ignorato
d:\WTL_INCLUDE\KWL\Misc\tracealloc.cpp(649) : fatal error C1903: impossibile recuperare l'errore o gli errori precedenti. Interruzione della compilazione

Have an idea to solve this probelm ?
Thanks

Luca Bonotto
GeneralRe: WTL project error Pin
Erik Rydgren4-Mar-04 20:44
Erik Rydgren4-Mar-04 20:44 
GeneralProblem in VC++ 6.0 Pin
sdaymond5-Jan-04 2:54
sdaymond5-Jan-04 2:54 
GeneralRe: Problem in VC++ 6.0 Pin
Tut813-Jan-04 2:15
Tut813-Jan-04 2:15 
GeneralRe: Problem in VC++ 6.0 Pin
Bananarama11-Mar-04 18:07
Bananarama11-Mar-04 18:07 
GeneralUnicode problem Pin
ThomT2-Jan-04 22:09
ThomT2-Jan-04 22:09 
GeneralRe: Unicode problem Pin
Erik Rydgren4-Jan-04 23:10
Erik Rydgren4-Jan-04 23:10 
GeneralAfxTrace error Pin
mickyp2-Jan-04 7:30
mickyp2-Jan-04 7:30 
GeneralRe: AfxTrace error Pin
Erik Rydgren4-Jan-04 23:08
Erik Rydgren4-Jan-04 23:08 
GeneralSmall fix required Pin
Michal Mecinski31-Dec-03 0:31
Michal Mecinski31-Dec-03 0:31 
GeneralRe: Small fix required Pin
Erik Rydgren31-Dec-03 7:21
Erik Rydgren31-Dec-03 7:21 
GeneralIt doesnt seem to work Pin
JaniOrca30-Dec-03 3:40
JaniOrca30-Dec-03 3:40 
GeneralRe: It doesnt seem to work Pin
Erik Rydgren30-Dec-03 3:55
Erik Rydgren30-Dec-03 3:55 
GeneralRe: It doesnt seem to work Pin
JaniOrca30-Dec-03 4:33
JaniOrca30-Dec-03 4:33 
GeneralRe: It doesnt seem to work Pin
Patje30-Dec-03 6:20
Patje30-Dec-03 6:20 
GeneralRe: It doesnt seem to work Pin
Johannes Gustafsson30-Dec-03 4:17
Johannes Gustafsson30-Dec-03 4:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.