On 11/02/2019 11:21, Michael Schnell wrote:
Yes, but how do I get notified, when the thread is gone, and I can free the
memory (of the object that was created)?
Ah, now I finally see the problem :)

The only idea that comes in my mind is creating yet another thread or a TTimer (by means 
of "QueueAsyncCall") to poll if the original Threads still living. This 
supposedly would involve OS calls for getting the thread ID and for checking same for 
validity. Ugly ! :( .

Then I may as well "hack" the memory manager.
Just wanted to see, if there was anything better. Given that MemManager.DoneThread exists, it should be possible to add a feature. Is that a plausible request?

I believe writing and installing your own memory manager is a documented feature, so the following steps should be future save. (unless something else install yet another mem manager after this. But since at that stage, code is already running and allocating memory, that should have happened before)

var
  CurMemMgr: TMemoryManager;
  CurDoneThreat: procedure;
threadvar
  logger: TMyThreadLogger; // might be created by each thread

procedure MyDoneThread;
begin
  Logger.Free;
  CurDoneThreat();
end;

  GetMemoryManager(CurMemMgr);
  CurDoneThreat := CurMemMgr.DoneThread;
  CurMemMgr.DoneThread := MyDoneThread;
  SetMemoryManager(CurMemMgr);

It is not the "nicest" solution but should work.

What I still need to find out is:
- Is DoneThread called under every OS/platform?
  but if not, it is not fatal, just a leak. Would be good to be able to have some IFDEF in this case (eg not create logger) - Is DoneThread called inside the thread? or otherwise with access to the correct threadvar "logger" instance?   and is that so on all OS/platform. (I can add tests, to protect against freeing the main threads object)

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to