[fpc-pascal] Question: Is there a global callback, when a thread gets terminated?

Martin Frb lazarus at mfriebe.de
Tue Feb 5 16:40:40 CET 2019


On 04/02/2019 14:49, Michael Schnell wrote:
>> Are threadvar variables being freed like regular managed vars
> Threadvars are not freed at all. They are like global vars, only that each running thread automatically has its own copy.
>
Yes, I know this. (did my google).
Even managed vars are not released and leak. And according to what I 
googled, same happens in Delphi.

My question was, how I can write my own code to free them.
There is a unit with functions (not methods), but they create an object 
(singleton). Without threads, that is dealt with by a unit finalization 
handler.
With out threads it just so happens that the finalization runs when the 
main thread ends.

But if you have such a singleton per thread?
The finalization runs to late, and it can not see the threadvar of other 
threads.
The TThread object is not accessible from that code, and if it was it 
has only one callback, that may already be used. (For this one would 
need "AddOnTerminate(AHandler)" instead of "property OnTerminate"

So my question was, if there is any other place, any hook, that is 
called at the time of a thread terminating? I haven't found one.

FPC seems to know when the thread ends. It calls 
MemoryManager.THreadDone. And it seems that is for all platforms?

How forward compatible would it be, to pretend to install your own 
memory manager? Simply creating a wrapper around the current one? Then 
you would get ThreadDone calls (which appear to runs inside the thread, 
or at least they see the correct threadvar)

Anything else?



More information about the fpc-pascal mailing list