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 was sure a great find.
What is FastMM?
FastMM is a memory manager replacement designed to be used with Delphi. 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?
1. Download the latest FastMM source code from http://sourceforge.net/projects/fastmm/
2. Copy the content of the downloaded zip file to a folder on your computer.
3. In Delphi add a path in Tools>Options>Library – Win32 Library Path to the FastMM folder that contains the unit FastMM4.pas.
4. 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
5. Optionally one can download and install the FastMM4 Options Interface program from JED software’s web site at
Once you have properly installed FastMM4 you will be able to detect any memory leaks and attempts to use freed memory. To take fully advantage of FastMM feature, you should open FastMM4Options.inc file and change some conditional compilation directive. This file is heavily commented to explain all the symbols. When developing your application, you should define the symbols "EnableMemoryLeakReporting" and "FullDebugMode". And If you enable this option your application will require the FastMM_FullDebugMode.dll library. 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 five different sections in this file.
1. Miscellaneous Options
This section contain general settings to control memory alignment, use of fast move library, multi threaded behavior and debug only when running the IDE
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 section 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 applications and DLLs.
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.
1. You can control how FastMM behaves in two ways:
a. By making changes to Option Grouping in the FastMM4Options.inc so you can define different FastMM behaviors for your release version and debug version.
b. 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.
2. 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.
3. 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.
4. 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:
a. In the Compiler options set the following options
· Debug Information
· Reference Info
· Use Debug DCus
b. In the Linker options make sure that one of the following options is set
· TD32 Debug info
· Map file
5. 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.
Popular posts from this blog
ShellExecute in Delphi – Launch external applications. ShellExecute is Delphi Windows API function that is mostly used for launch external applications from our Delphi application. This function is linked to the ShellExecute Windows API function. The function returns an integer that corresponds to an error code which is very useful when we need to show some status if the function worked or not . By using ShellExecute we can also do following operations.... Can print documents from within my program, without explicitly starting the application that created the document, such as: print a Word-document without starting Word. Can open browser with a local HTML page Can surf to a site i.e. open an external URL link from a Delphi application Can send mails thorugh outlook Syntax of Windows API function HINSTANCE ShellExecute( _In_opt_ HWND hwnd, _In_opt_ LPCTSTR lpOperation, _In_ LPCTSTR lpFile, _In_opt_ LPC
In this blog I will describe how to read and write data from and to an Excel file. Sometime in our application we use Excel for reporting purpose, for data import / export purpose and for other works. So here I will explain how to access an Excel file and use for data read / write. For this Excel 2003 or later should have installed in our system. First use Excel2000 unit to uses clause. This unit comes with Delphi installation it self. You can get the unit in installed path C:\Program Files (x86)\Embarcadero\RAD Studio\10.0\OCX\Servers Uses Excel2000; Before proceed I would mention an important word LCID which is required at most places. So what it LCID? LCID = In Microsoft Excel, the LCID indicates the currency symbol to be used when this is an xlListDataTypeCurrency type. Returns 0 (which is the Language Neutral LCID) when no locale is set for the data type of the column. We can get LCID in Delphi by using GetUserDefaultLCID function.. privat
Believe me, drawing shapes in Delphi is so easy. To develop a software like CAD, Paint, CorelDraw Delphi provides large number of classes and members that supports to draw shapes on a form or on a graphic control. In Delphi, we draw shapes on canvas of a form or graphic controls. Canvas is an area of form where we can draw shapes, lines and can fill colors on shapes. In Delphi, every form or graphic controls have Canvas property which provides TCanvas object that can be used to draw shapes. TPen object is used to draw lines and we can set size, color of lines. TBrush object is used to set color and style to fill the shapes. Most frequently used classes for drawing shapes are TCanvas , TBitmap, TGraphics, TPen, TBrush TCanvas Use TCanvas as a drawing surface for objects that draw an image of themselves TBitmap Bitmap is a powerful graphics object used to create, manipulate (scale, scroll, rotate, and paint), and store images in memory and as files on a disk. TGraphics TGr