Let us analyze your application - now have a free initial consultation.

Are you looking for a company to support or support your Delphi project?
Click here!

Jetzt Anfrage einreichenTelefon ERPwerk Button.fw

Memory Leaks in Delphi

Nils Eilers, 17.08.2017

Rad Studio 10.2.1.png

Here's the English translation:


Memory Leak Issue in Delphi 10.2 Tokyo Release 1 for Windows FireMonkey Applications

In Release 1 of Delphi 10.2 Tokyo, an issue has been reported regarding memory leaks in Windows FireMonkey applications. This problem has been noted in the Quality Portal as well as on StackOverflow.

The problem occurs because the callbacks passed to Thread.CurrentThread.ForceQueue in TStyledControl.KillResourceLink cannot be executed because the application closes before a thread can process them. The TThread class destructor destroys the list that still contains the unprocessed callbacks. The memory leaks likely do not occur during execution but rather when the program is terminated.


Proposed Solution by Stefan Glienke

Stefan Glienke, who also identified the cause of the problem, suggests a simple workaround:

By calling CheckSynchronize from System.Classes, the application waits for the cleanup threads to complete. This involves a brief synchronization with minimal noticeable delay.


Implementation Options

The relevant code can be placed in various locations. The simplest place is in the finalization block of the main form. With Memory Leak Reporting enabled, it might look like this:

initialization
  ReportMemoryLeaksOnShutdown := true;

finalization
  CheckSynchronize;

Alternatively, the change can be made in the DPR project file. For this, System.SysUtils and System.Classes must be added to the uses clause. The code can be structured as follows:

uses
  System.StartUpCopy,
  System.SysUtils,
  System.Classes,
  FMX.Forms,
  ...
  ;

{$R *.res}

procedure DoneApplication;
begin
  CheckSynchronize;
end;

begin
  AddExitProc(DoneApplication);
  Application.Initialize;
  ...;
  Application.Run;

Explanation

Using CheckSynchronize helps temporarily fix the issue until an official patch is released. It ensures that the application waits for any pending threads to complete their callbacks before fully shutting down.

This workaround is effective for minimizing memory leaks that occur when terminating the application.

Office

Delphi Entwickler PROFESSIONELLE ENTWICKLUNG, SCHLANKE LÖSUNGEN FÜR IHR PROJEKT

ERPwerk GmbH & Co. KG
Im Technologiepark 4
26129 Oldenburg

Phone: +49 441 777 729
E-Mail: 

Sozial Media

facebook Logo

Safety

Haftplicht exali