Click here to Skip to main content
6,822,613 members and growing! (18,329 online)
Email Password   helpLost your password?
Development Lifecycle » Debug Tips » General     Intermediate License: The MIT License

Memory leak finder

By Erik Rydgren

Custom memory handler with memory leak reporting and no-mans-land checking. Leaks are reported with call stack of allocation.
VC6, VC7, VC7.1Win2K, WinXP, Win2003VS.NET2003, Dev, QA
Posted:29 Dec 2003
Updated:10 Oct 2008
Views:149,892
Bookmarked:88 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
34 votes for this article.
Popularity: 4.91 Rating: 3.21 out of 5
8 votes, 23.5%
1
3 votes, 8.8%
2

3
8 votes, 23.5%
4
15 votes, 44.1%
5

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!

License

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

About the Author

Erik Rydgren


Member
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.
Occupation: Software Developer (Senior)
Location: Sweden Sweden

Other popular Debug Tips articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 78 (Total in Forum: 78) (Refresh)FirstPrevNext
Generalmemory leak finder for java. Pinmemberdpmpardhu23:11 9 Jun '09  
GeneralRe: memory leak finder for java. PinmemberErik Rydgren0:37 10 Jun '09  
GeneralWorks great with VS 6 C++ Pinmemberfeanorgem4:42 31 May '09  
NewsCode now upgraded to Visual Studio 2008 PinmemberErik Rydgren21:33 12 Oct '08  
GeneralRe: Code now upgraded to Visual Studio 2008 PinmemberMember 307553121:02 27 Nov '08  
GeneralRe: Code now upgraded to Visual Studio 2008 PinmemberEiswuxe213:38 16 Apr '09  
GeneralRe: Code now upgraded to Visual Studio 2008 PinmemberErik Rydgren23:59 16 Apr '09  
GeneralLinker Errors PinmemberSuper Garrison15:10 14 Jan '08  
GeneralRe: Linker Errors Pinmembermatakk@126.com19:22 10 Oct '08  
GeneralRe: Linker Errors PinmemberErik Rydgren21:29 12 Oct '08  
GeneralThanks. Pinmembermark-w15:55 12 Sep '07  
GeneralCompile errors in VC8 PinmemberDavid_Leikis10:16 5 Jun '07  
GeneralRe: Compile errors in VC8 PinmemberDavid_Leikis10:26 5 Jun '07  
GeneralTrouble in tracking memory leak PinmemberChandrasekar S3:20 10 Oct '05  
GeneralMissing __cdecl PinmemberJoão Paulo Figueira11:05 4 May '05  
GeneralFixing bugs PinmemberSergey Solozhentsev22:03 5 Dec '04  
GeneralTrouble Getting Leak Details PinsussAColombi20:24 5 Nov '04  
GeneralRe: Trouble Getting Leak Details PinmemberErik Rydgren21:30 7 Nov '04  
GeneralGetLastError() after new PinmemberMisanthrop6:48 7 Oct '04  
GeneralRe: GetLastError() after new PinmemberErik Rydgren5:17 8 Oct '04  
Generalwrong line numbers pointed Pinmemberbnujos21:51 29 Sep '04  
GeneralRe: wrong line numbers pointed PinmemberErik Rydgren23:53 29 Sep '04  
GeneralCompiler error with _UNICODE Pinmemberjpteruel23:23 27 Sep '04  
GeneralRe: Compiler error with _UNICODE PinmemberErik Rydgren0:19 28 Sep '04  
GeneralRe: Compiler error with _UNICODE Pinmemberjpteruel0:35 28 Sep '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 10 Oct 2008
Editor: Sean Ewington
Copyright 2003 by Erik Rydgren
Everything else Copyright © CodeProject, 1999-2010
Web17 | Advertise on the Code Project