In a given example I am receiving an exception when calling AThread.Free.
program Project44;
{$APPTYPE CONSOLE}
uses
SysUtils, Classes, Windows;
type
TMyException = class(Exception);
var
AThread: TThread;
begin
AThread := TThread.Create(True);
try
AThread.FreeOnTerminate := True;
//I want to do some things here before starting the thread
//During the setup phase some exception might occur, this exception is for simulating purpouses
raise TMyException.Create('exception');
except
AThread.Free; //Another exception here
end;
end.
I have two questions:
How should I free
AThreadinstance ofTThreadin a given example?I don't understand, why
TThread.Destroyis callingResumebefore destroing itself. What is the point of this?
You can't set
FreeOnTerminatetoTrueand callFreeon the thread instance. You have to do one or the other, but not both. As it stands your code destroys the thread twice. You must never destroy an object twice and of course when the destructor runs for the second time, errors occur.What happens here is that since you created the thread suspended, nothing happens until you explicitly free the thread. When you do that the destructor resumes the thread, waits for it to complete. This then results in
Freebeing called again because you setFreeOnTerminatetoTrue. This second call toFreecloses the handle. Then you return to the thread proc and that callsExitThread. This fails because the thread's handle has been closed.As Martin points out in the comment you must not create
TThreaddirectly since theTThread.Executemethod is abstract. Also, you should not useResumewhich is deprecated. UseStartto begin execution of a suspended thread.Personally I don't like to use
FreeOnTerminate. Using this feature results in the thread being destroyed on a different thread from which it was created. You typically use it when you want to forget about the instance reference. That then leaves you uncertain as to whether or not the thread has been destroyed when your process terminates, or even whether it is terminating and freeing itself during process termination.If you must use
FreeOnTerminatethen you need to make sure that you don't callFreeafter having setFreeOnTerminatetoTrue. So the obvious solution is to setFreeOnTerminatetoTrueimmediately before after callingStartand then forget about the thread instance. If you have any exceptions before you are ready to start then you can safely free the thread then since youFreeOnTerminatewould still beFalseat that point.A more elegant approach would be to move all the initialisation into the
TMyThreadconstructor. Then the code would look like this: