Thursday 8 December 2016

FastMM – Preparing your apps to report memory leaks

One of the most challenging parts of inheriting a legacy project is to fix the memory leaks that most often are hiding in the code. A while ago, while dealing with an application that managed to eat all the available memory within a few hours I found FastMM. And it sure was a great find.

What is FastMM?

FastMM is a memory manager replacement designed to be used with Delphi and C++ Builder. It is an Open Source project developed by Pierre Le Riche in South Africa.

Starting with Delphi 2006 FastMM replaced the Borland memory manager. Unfortunately, Delphi only ships with a subset of FastMM. Most of the useful debugging reporting that can be done with FastMM has been stripped from the shipping version of Delphi, RAD Studio and BDS.

But fear not! It is very simple to replace the stripped down version of FastMM with the full version. All you have to do is follow the directions outlined below. I have also included a step to install the FastMM4 Options Interface program. That program is a very friendly way of configuring the options contained in the file FastMM4Options.inc.

How to install FastMM

Download the latest FastMM source code from sourceforge.net or GitHub
Copy the content of the downloaded zip file to a folder on your computer.
In Delphi add a path in Tools>Options>Library – Win32 Library Path to the FastMM folder that contains the unit FastMM4.pas.
Copy the file FastMM_FullDebugMode.dll from the folder FastMM\FullDebugMode DLL\Precompiled to the Delphi install folder. For example in Delphi 2007 – C:\Program Files\CodeGear\RAD Studio\5.0\bin or in the Delphi XE install folder – C:\Program Files\Embarcadero\RAD Studio\8.0\bin
Optionally one can download and install the FastMM4 Options Interface program from JED software’s web site at jedqc
Configuring FastMM

Once you have properly installed FastMM4 you will be able to detect any memory leaks and attempts to use freed memory. But before we do that let’s take a look at what we have just installed.

In the FastMM folder you will find the file FastMM4Options.inc. This is the file that controls how FastMM behaves. Each option is very well documented and it is how you set the default behavior of FastMM. One can manually edit this file or, optionally, use the FastMM4 Options Interface.


Let’s take a quick look to this file. There are eight different sections in this file.

1 – Miscellaneous Options

This section contain general settings to control memory alignment, use of fastMove library, multithreaded behavior and debug only when running the ID

2 – Debugging Options

This section contains defines that control the debugging behavior of FastMM such as logging errors to a log file, dumping of memory along with an error, stack traces and more

3 – Memory Leak Reporting

This section controls the reporting of memory leaks, how to deal with expected memory leaks and the presence of the IDE or debug info to report errors.

4 – Instruction Set Options

This sections deals with using MMX instructions and this option currently only affects the variable size move routines.

5 – Memory Manager Sharing Options

This section allows sharing of the memory manager between a main application and DLLs.

6 – Option Grouping

Allows you to group a set of options for the release version and the debug version of your applications. So you can have the memory manager report issues when your application has been compiled for debugging and quietly ignore memory errors when compiled for release.

7 – Compilation Options For borlndmm.dll

If you’re compiling the replacement borlndmm.dll, set the defines in this section for the kind of DLL you require.

8 – Patch BCB Terminate

To enable the patching for BCB to make uninstallation and leak reporting.

What if I don’t want to mess with FastMM4Options.inc file?

Of course, you don’t have to edit the file manually. You can use FastMM4 Options Interface. However, you will not have access to the compilations options for debug and release version of your programs. Nevertheless, it is a very user friendly straightforward way of making changes to the default behavior of your application.

Switches are grouped in different tabs and for each option an detailed explanation (from FastMM4Options.inc) is displayed on the right pane.

Once you start the program you need to load the appropriate Option file, make the desired changes. Save the changes and rebuild your application.

Preparing and compiling projects in Delphi

There are a few simple steps to prepare your existing projects to use the full version of FastMM in a useful way.

You can control how FastMM behaves in two ways:
By making changes to Option Grouping in the FastMM4Options.inc so you can define different FastMM behaviors for your release version and debug version.
Or by using a conditional IFDEF statement to use the complete library on the debug version and ship the Delphi supplied library in the release version. To accomplish this, in Delphi open a project and add the unit FastMM4 as the first unit in the uses clause of the .dpr file.

uses
{$IFDEF DEBUG}
FastMM4,
{$ENDIF}
//DB,

Optionally, set FatsMM4 debug options using the FastMM4 Options Interface program. Make sure to build your program every time you make changes to any FastMM4 options.
In order for certain debug features of FastMM4 to work you must make sure that certain debug switches are turned on. The following is a list of recommended switches:
In the Compiler options set the following options
Debug Information
Reference Info
Use Debug DCUs
In the Linker options make sure that oneof the following options is set
TD32 Debug info
Map file
After you run the application a log of the memory manager can be found in the same folder where the application ran. The log file is named Leaks_MemoryManager_EventLog.txt.
If your project includes EXEs and DLLs you also need to define ShareMM, ShareMMIfLibrary and AttemptToUseSharedMM using the FastMM4 Options Interface program and add FastMM4.pas to the top of the uses section of the .dpr for both the main application and the DLL and follow the directions outlined in item 3. This will allow FastMM to report memory leaks across EXEs and DLLs.
What’s next?

That’s it! Now you are ready to detect memory errors like never before. Fire up your IDE and start plugging those leaks!

0 comments:

Post a Comment