[Lazarus] Memory leak with Synchronize on external threads

Sven Barth pascaldragon at googlemail.com
Tue May 31 22:22:29 CEST 2016


On 31.05.2016 14:37, Stefan Schalk wrote:
> There is a memory consumption/leak using the TThread.Synchronize function.
> 
> A callback function is called out of another ("external") thread (from an
> loaded DLL).
> Inside the callback function a synchronized call to a procedure has to be done.
> Every thing works without error, but there is a memory use increasement during
> the TThread.Synchronize call.
> 
> procedure OnExternalThreadCallback(...); cdecl;
> begin
>   ...
>   TThread.Synchronize(nil, @MyMainClass.CallbachSyncedWithMainThread);
>   ...
> end;
> 
> The external thread are created/used/freed like discussed in the Lazarus
> mailing list:
> Mutlithreaded Dll Callback crashes my Application
> <http://lists.lazarus.freepascal.org/pipermail/lazarus/2010-August/054728.html> 
> 
> The memory leak problem arises with Lazarus 1.6.0 and FPC 3.0.0.
> Under the hood the processing of the TThread.Synchronize changed.
> Lazarus 1.4.4 with FPC 2.6.4 didn't have this problem.
> I don't have the versions between 1.4.4 and 1.6.0, therefore I cannot say
> something about them (if there are any).

I've committed a fix in FPC revision 33863 and if all goes well it
should be merged to 3.0.1 as well, thus becoming part of 3.0.2.
A check with Delphi has shown that the WakeMainThread event gets Nil as
Sender argument with an external thread, so I can avoid the use of
CurrentThread which triggered the memory usage.
Please note that this is however not a leak as CurrentThread will create
a TExternalThread instance for a thread that hasn't been created by
TThread (and for which no instance exists yet) and this instance must
(sadly) be kept around until the program terminates.

Regards,
Sven





More information about the Lazarus mailing list