Tuesday, October 6, 2020

Deleaker: Advanced Memory Leak Detection for Delphi and RAD Studio

Beginning with RAD Studio 2006, Delphi, and RAD Studio in general, greatly benefited from the addition of Fast Memory Manager, or FastMM. And one of the truly welcome additions was the global variable ReportMemoryLeaksUponShutdown, which, when set to True and a small block memory leak is detected upon your application’s shutdown, displays a dialog box that identifies what was left behind. It doesn’t identify where the leak occurred, however, only that it happened.

FastMM continues to be available in RAD Studio, and you should consider downloading the full version from SourceForge (http://sourceforge.net/projects/fastmm/) to realize the full potential of FastMM, which includes much more than just memory leak detection, including enabling the generation of a stack trace, which may provide additional information about where the leak occurred.

That said, as far as memory leaks go, sometimes you need more. Which brings me to Deleaker.

I recently had the opportunity to try out Deleaker from Softantics in my Delphi applications, and I was impressed. Deleaker is a memory leak detection tool for Windows 32-bit and 64-bit applications. It can integrate with RAD studio and Visual Studio (as well as Qt Creator), and also supports a command line interface that permits you to include Deleaker as part of your continuous integration (CI) process.

In the past I’ve relied on RAD Studio’s ReportMemoryLeaksOnShutdow to identify memory leaks, but that option only tells you that you have leaks as well as how much memory was leaked. By comparison, Deleaker, when property configured, does an amazing job of identifying the source of the leak, going so far as to identify unit names and even line numbers where the leak originated. It can also find leaks in any module, for example, WinAPI functions such as CoTaskMemAlloc. FastMM cannot find that kind of leak.

Installation is simple, and after installation a new menu item, Deleaker, appears in RAD Studio’s main menu. From that menu you can enable Deleaker for all of your applications, configure Deleaker’s options, and open the Deleaker Console.

When the installation of Deleaker is complete it displays a help file that explains how to configure RAD Studio to generate the information Deleaker needs in order to serve you. (In short, you need to turn on your compiler and linker debugging options.) This help file, which can also be displayed any time you need it from the Deleaker Console, is well written and easy to follow, and the setup in RAD Studio only requires a few easy steps.

When Deleaker is enabled, and you are running your application with debugging enabled, the Deleaker Console launches when you shutdown your application. If no leak is detected, it reports that. Alternatively, if a leak is detected, Deleaker can often identify the unit name and line number where the leak likely originated. If the leak is related to a Delphi object, Deleaker will report what class of object produced the leak. This can save you hours of time scrutinizing your code for mishandled resources.

You can also display the Deleaker Console while your application is running. This permits you to take one or more snapshots of your memory usage, so you can see what’s going on while your application is still running. The Deleaker Console also lets you compare snapshots, which permits you to see changes in memory usage over time.

The types of leaks that Deleaker can detect are extensive. Here is a list, taken from the Deleaker Help file:

Memory: memory blocks allocated by heap functions, virtual memory, OLE memory:
BSTR, SAFEARRAY, etc
GDI: HBITMAP, HDC, HPEN, etc.
User32: HICON, HCURSOR, etc.
Handles: file handles, events, mutexes, etc.
FileView: views of file mappings
ActCtxCookie: cookies of activation contexts
Fiber: fibers made by CreateFiber, etc.
CriticalSection: critical sections ((if one has called InitializeCriticalSection but
forgotten to call DeleteCriticalSection)
EnvironmentString: ANSI and UNICODE environment strings
FlsSlot: fls slots
TlsSlot: tls slots
Atom: atoms.

During development, when Deleaker is enabled, you need to run your application with debugging enabled. However, it is also possible to use Deleaker with an application that is running outside of Delphi. For example, on a machine on which Delphi is not installed. Simply copy the Deleaker Console onto the machine, and then run it (Microsoft’s .NET Framework 3.5 might need to be installed). From there you can either launch your application from the Deleaker Console, or even attach to your already running application.

You can check out Deleaker free for 14 days, after which you can buy a license. Available licenses include a home license (for personal use), a single developer license, and site licenses.

You can find the Deleaker home page at https://www.deleaker.com/

Copyright © 2020. All Rights Reserved.