TidyHeap
Volume Number: 13
Issue Number: 5
Column Tag: develop
Squashing Memory Leaks With TidyHeap
by Mike Fullerton
One of the more mundane aspects of application development is making sure any system
resources that your application allocates get deallocated, particularly memory
allocations. Think of the hours you've spent tracking down pesky memory leaks. A
powerful debugging and QA tool, TidyHeap watches over memory allocations and helps
you track down leaky blocks. Learn how to make sure that memory allocated by your
application is always managed correctly.
TidyHeap is a tool for tracking and verifying dynamically allocated memory in C++
applications. It started out as a quick and dirty way to keep track of undeleted objects
in the MacApp framework and evolved into a tool for anyone writing application
software in C++. TidyHeap works with any application built in C++, with or without a
framework. Among its powerful features are the following:
• If your application allocates an object or data that's never deleted,
TidyHeap tells you about it when your application quits. (Objects and data
allocated by your application are called client objects in this article.)
• TidyHeap can provide you with the filename and line number where any
existing client object was created, including a client object that's never
deleted.
• You can tell TidyHeap to watch for double deletions on all client objects or
on a specific client object.
• You can verify the integrity of a single client object or all client objects
at each pass through the event loop.
• You can have TidyHeap break into MacsBug when a specific client object is
deleted.
• You can force every single new operation to fail in consecutive order, in
consecutive runs of your application.
• You can design your own tests for specific client objects or for all client
objects.
In short, TidyHeap is designed to be flexible, powerful, and user extensible.
Currently, TidyHeap runs only on the PowerPC(tm) platform (if you would like to see
it implemented for 680x0 machines, send e-mail to mabugs@devtools.apple.com), and
it's compatible only with Metrowerks CodeWarrior (we MacAppsters plan to make it
compatible with the various MPW compilers sometime soon). A package of TidyHeap
materials is available at http://www.mactech.com.
TidyHeap Basics
TidyHeap itself is a small library of C++ classes that intercept and track all
allocations and deallocations performed by the global operators new and delete. It does
this invisibly - you don't need to do anything but call new and delete in the way you
usually do. Also, since TidyHeap is essentially a debugging tool, it's compiled and
enabled only if its preprocessor constant qTidyHeap is defined to true. This enables you
to turn it off easily when building nondebug versions of your application.
TidyHeap works with any data type that can be created with new. This can be all the
objects in your application or framework, structs created to be passed to Toolbox
routines, or huge char arrays created as buffers. It really doesn't matter. TidyHeap
treats all allocations as raw blocks of allocated memory, without knowing or caring
what the user data is.
The raw materials