Click here to Skip to main content
15,879,535 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 344.9K   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

 
QuestionCrash In WriteStackTrace Pin
Sivaji156517-Apr-14 22:55
Sivaji156517-Apr-14 22:55 
QuestionSolution updated to Visual Studio 2010 and inclusion of fixes by PEK Pin
Erik Rydgren22-Feb-12 13:07
Erik Rydgren22-Feb-12 13:07 
AnswerRe: Solution updated to Visual Studio 2010 and inclusion of fixes by PEK Pin
Rohit Dubey from Hyderabad3-Mar-12 13:39
Rohit Dubey from Hyderabad3-Mar-12 13:39 
QuestionSomething like Pin
Mast Avalons4-Jan-12 5:14
Mast Avalons4-Jan-12 5:14 
AnswerRe: Something like Pin
JohnDepth20-Feb-12 6:32
JohnDepth20-Feb-12 6:32 
GeneralRe: Something like Pin
Erik Rydgren20-Feb-12 10:07
Erik Rydgren20-Feb-12 10:07 
GeneralRe: Something like Pin
Member 428961327-Feb-12 3:29
Member 428961327-Feb-12 3:29 
GeneralRe: Something like Pin
SuperMegaCoder14-Mar-12 6:23
SuperMegaCoder14-Mar-12 6:23 
GeneralRe: Something like Pin
Mast Avalons5-Jun-12 12:19
Mast Avalons5-Jun-12 12:19 
GeneralRe: Something like Pin
JohnDepth13-Jul-12 8:11
JohnDepth13-Jul-12 8:11 
GeneralBugfix: GetStackTrace writing data in nomans land, and some improvements. Pin
PEK18-Mar-10 20:24
PEK18-Mar-10 20:24 
GeneralRe: Bugfix: GetStackTrace writing data in nomans land, and some improvements. Pin
Tardi S1-Jun-10 3:16
Tardi S1-Jun-10 3:16 
Generalmemory leak finder for java. Pin
dpmpardhu9-Jun-09 22:11
dpmpardhu9-Jun-09 22:11 
GeneralRe: memory leak finder for java. Pin
Erik Rydgren9-Jun-09 23:37
Erik Rydgren9-Jun-09 23:37 
GeneralWorks great with VS 6 C++ Pin
feanorgem31-May-09 3:42
feanorgem31-May-09 3:42 
NewsCode now upgraded to Visual Studio 2008 Pin
Erik Rydgren12-Oct-08 20:33
Erik Rydgren12-Oct-08 20:33 
GeneralRe: Code now upgraded to Visual Studio 2008 Pin
Member 307553127-Nov-08 20:02
Member 307553127-Nov-08 20:02 
GeneralRe: Code now upgraded to Visual Studio 2008 Pin
Eiswuxe216-Apr-09 12:38
Eiswuxe216-Apr-09 12:38 
GeneralRe: Code now upgraded to Visual Studio 2008 Pin
Erik Rydgren16-Apr-09 22:59
Erik Rydgren16-Apr-09 22:59 
GeneralLinker Errors Pin
Super Garrison14-Jan-08 14:10
Super Garrison14-Jan-08 14:10 
GeneralRe: Linker Errors Pin
matakk10-Oct-08 18:22
matakk10-Oct-08 18:22 
GeneralRe: Linker Errors Pin
Erik Rydgren12-Oct-08 20:29
Erik Rydgren12-Oct-08 20:29 
GeneralThanks. Pin
mark-w12-Sep-07 14:55
mark-w12-Sep-07 14:55 
GeneralCompile errors in VC8 Pin
David_Leikis5-Jun-07 9:16
David_Leikis5-Jun-07 9:16 
GeneralRe: Compile errors in VC8 Pin
David_Leikis5-Jun-07 9:26
David_Leikis5-Jun-07 9:26 

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.