Search code examples
multithreadingdelphidelphi-7

In a Delphi thread's, when the memory is freed?


A thread has assigned

FreeOnTerminate := true;

I understand that it's memory wil be freed when it terminates, but when does the Terminate event will happen?

Or, in a more generic way, when is the memory related to some thread freed?


Solution

  • When FreeOnTerminate is true, TThread frees its own memory (calls its own destructor on itself) after TThread.DoTerminate() has been called and exited, after TThread.Execute() has been called and exited. So basically, once DoTerminate() has been called (which internally triggers the TThread.OnTerminate event, if assigned) and exited, assume your TThread object is no longer valid if FreeOnTerminate is true.