LeakTracer - trace and analyze memory leaks in C++ programs

Oct 2011 UPDATE: Frédéric Germain has setup a Github page containing all the below patches and additional new features, see: https://github.com/fredericgermain/LeakTracer. This is the newest version; all the below source code and documentation are of historic interest only.

LeakTracer is a small tool I wrote when checking a C++ program for memory leaks. I couldn't get dmalloc to display what I wanted, and I just saw the __builtin_return_address gcc-extension mentioned.

To use LeakTracer, run your program using the provided LeakCheck script. It uses the LD_PRELOAD feature to "overlay" some functions on top of your functions (no recompile needed). If your platform does not support LD_PRELOAD, you can add the LeakTracer.o object file to the objects in your Makefile and run your application.

LeakTracer uses gdb to print out the exact line where the memory was allocated and not freed - this of course means you have to free all dynamically allocated data. LeakTracer also overrides the global operator new and operator delete - this will give problems if you override them as well.

LeakTracer traces only new/new[] and delete calls - it does not look at malloc/free/realloc.

Read the full README file included in the package for more information.


LeakTracer source: ( 10K) v2.4 LeakTracer.tar.gz

LeakTrace works on my Linux system and has been reported to work on Solaris as well as HP-UX. Because of its simplicity it has gained some surprising popularity and has seen use in e.g. KDE and Mozilla.

License: Public Domain (do with it whatever you want).

[New!] EXTENDED VERSION: Michael Gopshtein has completely rewritten LeakTracer to make it more efficient and flexible, monitor specific threads and display the ful stack trace. Download LeakTracer-mg.tar.gz.

LeakTracer is now part of Debian (testing/unstable), just apt-get install leaktracer, courtesy of Yann Dirson.

What's new
Nov 16 A few user contributions to LeakTracer
Sep 19 The wonderful world of CSS
Feb 13 New XEmacs elisp -- C/C++ utilities

Site navigation

External links for this page
ccmalloc - Shows full function traceback
valgrind - powerful tool combines strace/efence/leak tracing

User Contributions

Sadly, time constraints have not yet permitted me to integrate these into the main LT package:

[New!] leaktracer-freestackopt.diff - submitted by Robin Hiroko Walsh of TextDigger, this patch optimises LeakTracer's freelist searching, improving performance 40-fold in certain cases.

Patch for capturing and presenting multiple stack levels by José María González: ( 12K) leaktracer.all.diff -- this includes many other features, see mail.txt.

TK front-end by Andrew Ogley: (2.7K) v2.2 LeakTool-1.1.tgz

Excel macro for presenting LT results by Amir Kirsh and Ido Zilbergled: ( 13K) OrganizeLeakTracerOutput.zip

ARM patch by Patrick: leaktracer-on-arm.diff

Patch for tracing malloc/free too by Greg Stewart: LeakTracer-mf.cc (save as LeakTracer.cc and add -ldl to libraries in Makefile)

Another patch from Боби Б. for MIPS support: LeakTracer-MIPS.patch.

Example output

~/p/arc# ea/LeakTracer/leak-analyze ./arc
Gathered 8 (8 unique) points of data.
Allocations: 1 / Size: 36
0x80608e6 is in NullArcableInstance::NullArcableInstance(void) (Machine.cc:40).
39      public:
40          NullArcableInstance() : ArcableInstance(new NullArcable) {}

Allocations: 1 / Size: 8 0x8055b02 is in init_types(void) (Type.cc:119). 118 void init_types() { 119 Type::Integer = new IntegerType;

Allocations: 1 / Size: 132 (new[]) 0x805f4ab is in Hashtable<NativeCallable, String, false, true>::Hashtable(unsigned int) (ea/h/Hashtable.h:15). 14 Hashtable (uint _size = 32) : size(_size), count(0) { 15 table = new List<E, own> [size];

Version history

February 23, 1999               v1.1 - first public release
November 19, 2000               v2.0 - Rewrite by Henner Zeller introduces LD_PRELOAD
                                       and much more
February 27, 2001               v2.1 - Further update by Henner: optional thread safety,
                                       choose what should make LeakTracer abort(), better
                                       tracing of delete on non-new'ed pointers
August 28, 2003                 v2.4 - Improve MAGIC on platforms that don't allow unaligned