Memory Leaks in Delphi
Nils Eilers, 17.08.2017

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.



