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

OBones obones at free.fr
Tue Feb 5 09:24:15 CET 2019


Hello,

If you are targeting Windows, there is a special area in the PE 
structure for a DLL that contains addresses that will be jumped to 
whenever a thread gets created/destroyed. This has been used by 
libtcmalloc from gperftools:
https://github.com/gperftools/gperftools/blob/master/src/windows/port.cc

I don't know how to populate the appropriate locations in the PE header 
with freepascal though.
The comments in that same file hint that there is a function called 
pthread_key_create for systems that implement the pthread library, but I 
have not looked into that.

If you want an entirely FPC based solution, I would patch the running 
exe so that the system thread routine that calls OnTerminate jumps to my 
override which calls the original one before doing any finalization work 
that it needs to do.
But installing such a trampoline is very platform specific, I only know 
how to do it on Intel based architectures.



More information about the fpc-pascal mailing list