This sample demonstrates how to use the Delphi leak detection mechanism.
You can find the Leak Reporting sample project at:
- Start > Programs > Embarcadero RAD Studio > Samples > Delphi > RTL > Leak Reporting
- Subversion Repository for Delphi: http://radstudiodemos.svn.sourceforge.net/viewvc/radstudiodemos/branches/RadStudio_XE2/Delphi/RTL/Leak%20Reporting/
This application creates a form with three buttons and a combo box. The combo box allows the user to choose whether the memory leaks will be reported or not when the application closes.
The first button, Leak a TObject, creates a new instance of TObject. Clicking this button prompts a notification when the application closes, if the Unexpected memory leaks will be reported on shutdown option is selected from the combo box.
The second button also creates a new instance of TObject but registers it as an expected memory leak, therefore no notification will be prompted when the application closes.
The third button creates a TObject instance, registers it as an expected memory leak, then it frees the allocated memory and unregisters the memory leak. Clicking this button will also not trigger a notification when the application closes.
How to Use the Sample
- Navigate to Start > Programs > Embarcadero RAD Studio > Samples and open LeakTest.dproj.
- Press F9 or choose Run > Run.
This project has one source file, LeakTestForm, which contains the class for the main form.
TfLeakTestForm, the class for the main form, creates and destroys TObject instances, registers and unregisters memory leaks, and sets whether memory leaks will be reported or not on application shutdown.
- RegisterExpectedMemoryLeak is used to indicate the memory location of an object that is not expected to be freed. The reverse is achieved using UnregisterExpectedMemoryLeak.
- The Boolean variable ReportMemoryLeaksOnShutdown is used to set the way the memory leaks are reported when the application closes.