ARC(iOS)下的Delphi TThread尚未发布

时间:2022-04-14 09:44:05

What is a proper way to terminate a thread using Delphi for iOS under ARC management?

在ARC管理下使用Delphi for iOS终止线程的正确方法是什么?

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:

好吧,按下Button1将产生一个线程。线程启动后,如果单击Button2,它将显示RefCount值3!好吧,1是我的FThread变量的引用,并且TThread内部创建了2个额外的引用...我已经深入研究了源代码并发现RefCount在这里增加了:

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).

好吧......线程完成后(在我的情况下,5秒后),RefCount将减少到2(因为我已将FreeOnTerminate设置为TRUE,但如果我没有将FreeOnTerminate设置为TRUE,则RefCount仍为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...

看到问题了吗?线程永远不会完成,并且永远不会调用析构函数,如果我调用FThread:= nil,那么,RefCount应该从2减少到1(或者在FreeOnTerminate = FALSE的情况下从3减少到2),并且线程永远不会在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?

也许我错过了一些东西,因为我习惯于没有ARC的线程......所以,我在这里缺少什么?或者在ARC下的TThread实现中是否存在错误?

Maybe this definition of TThread

也许这个TThread的定义

private class threadvar
  FCurrentThread: TThread;

should be something like

应该是这样的

private class threadvar
  [Weak] FCurrentThread: TThread;

1 个解决方案

#1


3  

After some digging in qc the following issues and workaround show up:

在qc中进行了一些挖掘之后,出现了以下问题和解决方法:

Thread parameters should be passed as const

线程参数应该作为const传递

function ThreadProc(Thread: TThread): Integer;  <<-- pass_by_reference pushes
var                                                  up the ref_count.
  FreeThread: Boolean;
begin
  TThread.FCurrentThread := Thread;

Had you passed it as const the ref_count would have not gone up to 3. Normally this is not a problem, because the ref_count gets decreased on exit of the function, but here:

如果你把它作为const传递,ref_count就不会达到3.通常这不是问题,因为ref_count在退出函数时会减少,但是这里:

the function epilog is never exectued because pthread_exit() jumps out of the code.

函数epilog永远不会被执行,因为pthread_exit()跳出代码。

This is only part of the solution though, quite a bit more needs to be done...

这只是解决方案的一部分,还需要做更多的工作......

Full workaround by Dave Nottage

Dave Nottage的完整解决方法

After much fiddling around, I've come up with this potential workaround:

经过多次摆弄后,我想出了这个潜在的解决方法:

Make these mods to the Classes unit: Change:

将这些mod设置为Classes单元:更改:

function ThreadProc(Thread: TThread): Integer;

to:

function ThreadProc(const Thread: TThread): Integer;

and add:

TThread.FCurrentThread := nil;

after this line:

在这一行之后:

if FreeThread then Thread.Free;

Override DoTerminate in the TThread descendant, thus:

在TThread后代中覆盖DoTerminate,因此:

procedure TMyThread.DoTerminate;
begin
  try
    inherited;
  finally
    __ObjRelease;
  end;
end;

Call the thread thus:

因此调用线程:

FMyThread.Free; // This will do nothing the first time around, since the reference will be nil
FMyThread := TMyThread.Create(True);
// DO NOT SET FreeOnTerminate
FMyThread.OnTerminate := ThreadTerminate;
FMyThread.Resume;

This (at least for me, on the device) results in the thread being destroyed on subsequent calls.

这(至少在我的设备上)会导致线程在后续调用中被破坏。

NOTE: Under ARC conditions, never declare a reference to a thread locally, because when it falls out of scope, the thread is destroyed and the Execute method is never called, not to mention other problems it causes.

注意:在ARC条件下,永远不要在本地声明对线程的引用,因为当它超出范围时,线程将被销毁,并且永远不会调用Execute方法,更不用说它导致的其他问题。

#1


3  

After some digging in qc the following issues and workaround show up:

在qc中进行了一些挖掘之后,出现了以下问题和解决方法:

Thread parameters should be passed as const

线程参数应该作为const传递

function ThreadProc(Thread: TThread): Integer;  <<-- pass_by_reference pushes
var                                                  up the ref_count.
  FreeThread: Boolean;
begin
  TThread.FCurrentThread := Thread;

Had you passed it as const the ref_count would have not gone up to 3. Normally this is not a problem, because the ref_count gets decreased on exit of the function, but here:

如果你把它作为const传递,ref_count就不会达到3.通常这不是问题,因为ref_count在退出函数时会减少,但是这里:

the function epilog is never exectued because pthread_exit() jumps out of the code.

函数epilog永远不会被执行,因为pthread_exit()跳出代码。

This is only part of the solution though, quite a bit more needs to be done...

这只是解决方案的一部分,还需要做更多的工作......

Full workaround by Dave Nottage

Dave Nottage的完整解决方法

After much fiddling around, I've come up with this potential workaround:

经过多次摆弄后,我想出了这个潜在的解决方法:

Make these mods to the Classes unit: Change:

将这些mod设置为Classes单元:更改:

function ThreadProc(Thread: TThread): Integer;

to:

function ThreadProc(const Thread: TThread): Integer;

and add:

TThread.FCurrentThread := nil;

after this line:

在这一行之后:

if FreeThread then Thread.Free;

Override DoTerminate in the TThread descendant, thus:

在TThread后代中覆盖DoTerminate,因此:

procedure TMyThread.DoTerminate;
begin
  try
    inherited;
  finally
    __ObjRelease;
  end;
end;

Call the thread thus:

因此调用线程:

FMyThread.Free; // This will do nothing the first time around, since the reference will be nil
FMyThread := TMyThread.Create(True);
// DO NOT SET FreeOnTerminate
FMyThread.OnTerminate := ThreadTerminate;
FMyThread.Resume;

This (at least for me, on the device) results in the thread being destroyed on subsequent calls.

这(至少在我的设备上)会导致线程在后续调用中被破坏。

NOTE: Under ARC conditions, never declare a reference to a thread locally, because when it falls out of scope, the thread is destroyed and the Execute method is never called, not to mention other problems it causes.

注意:在ARC条件下,永远不要在本地声明对线程的引用,因为当它超出范围时,线程将被销毁,并且永远不会调用Execute方法,更不用说它导致的其他问题。