Click here to Skip to main content
Click here to Skip to main content

Memory leak finder

By , 22 Feb 2012
Rate this:
Please Sign up or sign in to vote.

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

About the Author

Erik Rydgren
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

 
GeneralMissing __cdecl PinmemberJoão Paulo Figueira4-May-05 10:05 
GeneralFixing bugs PinmemberSergey Solozhentsev5-Dec-04 21:03 
GeneralTrouble Getting Leak Details PinsussAColombi5-Nov-04 19:24 
GeneralRe: Trouble Getting Leak Details PinmemberErik Rydgren7-Nov-04 20:30 
GeneralGetLastError() after new PinmemberMisanthrop7-Oct-04 5:48 
GeneralRe: GetLastError() after new PinmemberErik Rydgren8-Oct-04 4:17 
Generalwrong line numbers pointed Pinmemberbnujos29-Sep-04 20:51 
GeneralRe: wrong line numbers pointed PinmemberErik Rydgren29-Sep-04 22:53 
GeneralCompiler error with _UNICODE Pinmemberjpteruel27-Sep-04 22:23 
GeneralRe: Compiler error with _UNICODE PinmemberErik Rydgren27-Sep-04 23:19 
GeneralRe: Compiler error with _UNICODE Pinmemberjpteruel27-Sep-04 23:35 
GeneralRe: Compiler error with _UNICODE PinmemberErik Rydgren27-Sep-04 23:40 
GeneralRe: Compiler error with _UNICODE Pinmemberjpteruel28-Sep-04 2:22 
Generalusing memory leak finder in a project with multiple dlls PinmemberKevin Cao20-Sep-04 20:24 
GeneralRe: using memory leak finder in a project with multiple dlls PinmemberErik Rydgren22-Sep-04 3:26 
GeneralRe: using memory leak finder in a project with multiple dlls PinmemberDieter Hammer23-Sep-04 5:28 
GeneralRe: using memory leak finder in a project with multiple dlls PinmemberErik Rydgren23-Sep-04 5:40 
GeneralRe: using memory leak finder in a project with multiple dlls PinmemberCurtis W23-Sep-04 7:25 
GeneralRe: using memory leak finder in a project with multiple dlls PinmemberErik Rydgren23-Sep-04 20:27 
GeneralRe: using memory leak finder in a project with multiple dlls PinmemberCurtis W23-Sep-04 21:47 
GeneralRe: using memory leak finder in a project with multiple dlls PinsussJaniOrca24-Sep-04 1:36 
GeneralRe: using memory leak finder in a project with multiple dlls PinmemberJonas O15-Oct-04 4:34 
GeneralRe: using memory leak finder in a project with multiple dlls PinmemberErik Rydgren15-Oct-04 4:41 
Generalmemory decrease PinsussAnonymous28-Jul-04 17:56 
GeneralRe: memory decrease PinmemberErik Rydgren15-Aug-04 20:17 
GeneralRe: memory decrease PinmemberEarl Allen2-Oct-04 16:05 
GeneralUnhandled Exception Pinmembersabre_sg27-May-04 23:04 
GeneralRe: Unhandled Exception PinmemberErik Rydgren27-May-04 23:15 
GeneralRe: Unhandled Exception Pinmembersabre_sg30-May-04 20:47 
GeneralRe: Unhandled Exception PinmemberErik Rydgren31-May-04 20:27 
GeneralRe: Unhandled Exception PinsussAnonymous1-Jun-04 1:11 
GeneralRe: Unhandled Exception Pinmembersabre_sg1-Jun-04 1:14 
GeneralRe: Unhandled Exception PinmemberEarl Allen2-Oct-04 15:52 
Generalfatal error C1010: unexpected end of file while looking for precompiled header directive Pinmembersurajama28-Apr-04 19:12 
GeneralRe: fatal error C1010: unexpected end of file while looking for precompiled header directive PinmemberErik Rydgren10-May-04 1:33 
GeneralRe: fatal error C1010: unexpected end of file while looking for precompiled header directive PinsussDavid Haley8-Aug-04 9:51 
GeneralRe: fatal error C1010: unexpected end of file while looking for precompiled header directive PinmemberDavid Haley8-Aug-04 9:56 
GeneralRe: fatal error C1010: unexpected end of file while looking for precompiled header directive PinmemberErik Rydgren21-Sep-04 21:35 
GeneralNo line number information PinmemberJulius Penn9-Mar-04 4:06 
GeneralRe: No line number information PinmemberBananarama11-Mar-04 18:14 
GeneralRe: No line number information Pinmembermasterofx3220-Dec-04 13:04 
GeneralRe: No line number information PinmemberKikimora1-Apr-05 3:07 
GeneralRe: No line number information Pinmembergualo27-Apr-05 3:19 
GeneralRe: No line number information Pinmembergualo27-Apr-05 3:58 
GeneralRe: No line number information Pinmemberstamari29-Jan-07 5:56 
GeneralRe: No line number information Pinmembermcanti3-Feb-07 22:41 
GeneralRe: No line number information PinmemberBeakerBob5-Nov-07 6:00 
GeneralRe: No line number information Pinmembermark_xlprint9-May-05 21:51 
GeneralWTL project error PinmemberKillerluke4-Mar-04 6:27 
GeneralRe: WTL project error PinmemberErik Rydgren4-Mar-04 20:44 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 22 Feb 2012
Article Copyright 2003 by Erik Rydgren
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid