Workaround for FMX Memory Leaks on Windows in 10.2.1

by Aug 16, 2017

Many of our customers today write FireMonkey Windows applications. We recently got some customer reports on FMX memory leaks on Windows using RAD Studio 10.2 Release 1 (or Tokyo Release 1). The issue is reported at https://quality.embarcadero.com/browse/RSP-18774 and https://stackoverflow.com/questions/45588741/delphi-recompiling-application-with-10-2-1-causes-memory-leaks.
If you encounter such issues after installing 10.2.1, we recommend trying the workaround below. We will address this issue in the future.

To our knowledge, the memory leak doesn't happen during program execution, but on termination because a cleanup thread does not excute in time. Delphi MVP Stefan Glienke suggested a relatively simple workaround both on the Quality Portal bug report and in the StackOverflow page linked above. By calling
CheckSynchronize
(from System.Classes) you can have the application wait for the cleanup threads to execute. This does a quick sync, but does not cause any noticeable delay. 

There are different places where you can put this code, but the simplest one for me is a finalization block in the main form source code (no need to update the library units). This is an example of the code added to the end of the main form unit, along with activating memory leak reporting:



Another very good suggestion is to change the project file (the .dpr file), but your mileage might vary — you can also consider fixing the library as this will fix all of your applications at once.