What is a proper way to terminate a thread using Delphi for iOS under ARC management?
Take this simple example:
TMyThread = class(TThread)
protected
procedure Execute; override;
public
destructor Destroy; override;
end;
TForm2 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
FThread: TMyThread;
public
end;
{ TMyThread }
destructor TMyThread.Destroy;
begin
ShowMessage('Destroy');
inherited Destroy;
end;
procedure TMyThread.Execute;
begin
Sleep(5000);
end;
{ TForm2 }
procedure TForm2.Button1Click(Sender: TObject);
begin
FThread := TMyThread.Create(TRUE);
FThread.FreeOnTerminate := TRUE;
FThread.Start;
end;
procedure TForm2.Button2Click(Sender: TObject);
begin
ShowMessage(FThread.RefCount.ToString);
end;
procedure TForm2.Button3Click(Sender: TObject);
begin
FThread := nil;
end;
Ok, pressing Button1 will spawn a thread. After thread is started, if you click Button2 it will display a RefCount value of 3!! Well, 1 is the reference to my FThread variable, and there are 2 additional references that TThread creates internally... I have digged into source code and found that RefCount is increased here:
constructor TThread.Create(CreateSuspended: Boolean);
ErrCode := BeginThread(nil, @ThreadProc, Pointer(Self), FThreadID);
if ErrCode <> 0 then
raise EThread.CreateResFmt(@SThreadCreateError, [SysErrorMessage(ErrCode)]);
{$ENDIF POSIX}
And here:
function ThreadProc(Thread: TThread): Integer;
var
FreeThread: Boolean;
begin
TThread.FCurrentThread := Thread;
Well... After thread is finished (In my case, after 5 seconds), the RefCount will decrease to 2 (Because I have set FreeOnTerminate to TRUE, but if I don´t set FreeOnTerminate to TRUE, the RefCount will still be 3).
See the problem? Thread is never finished and destructor is never called, if I call FThread := nil
, then, the RefCount should decrease from 2 to 1 (Or from 3 to 2 in case FreeOnTerminate = FALSE
), and thread is never released under ARC...
Maybe I´m missing something because I´m used to working with threads with no ARC... So, what am I missing here? Or is there a bug in TThread implementation under ARC?
Maybe this definition of TThread
private class threadvar
FCurrentThread: TThread;
should be something like
private class threadvar
[Weak] FCurrentThread: TThread;