On windows, to stop and destroy an anonymous thread, i simply do FmyTask.free => that will call destroy => and that inside the destroy will set terminate = true and call waitfor to wait the task is finished => and finally clean the memory used
but on ARC, everything is different 🙁
i use this code :
destructor Destroy; override;
FMyTask.free; // << will do nothing because FMyTask.refcount = 2 !! how it’s possible ?
FMyTask.FreeOnTerminate := False;
and i do nothing else than just
MyObject := TmyObject.create;
MyObject := nil;
but as you see, in the onDestroy of TmyObject, FMyTask have a refcount of 2 !! so it’s mean that the FMyTask will be not destroyed (refcount will be decreased to 1) with TmyObject (but later) and you imagine all the bug that can results 🙁
it’s sean that a reference to fMyTask is keep inside this function :
function ThreadProc(const Thread: TThread): Integer;
Thread.__ObjAddRef; // this ensures the instance remains for as long as the thread is running
TThread.FCurrentThread := Thread;
if Thread.FSuspended then
// Register the auto release pool
pool := objc_msgSend(objc_msgSend(objc_getClass(‘NSAutoreleasePool’),
Thread.FStarted := True;
if not Thread.Terminated then
Thread.FFatalException := AcquireExceptionObject;
Result := Thread.FReturnValue;
FreeThread := Thread.FFreeOnTerminate;
Thread.FFinished := True;
if FreeThread then
Thread.__ObjRelease; // This will clear the thread reference that was added by setting FreeOnTerminate.
Thread.__ObjRelease; // This will clear the thread reference we added above. This may initiate disposal.
// Destroy Collator Cache
// Last thing to do in thread is to drain the pool
// Detach the NativeActivity virtual machine to ensure the proper relase of JNI context attached to the current thread
// Directly call pthread_exit since EndThread will detach the thread causing
// the pthread_join in TThread.WaitFor to fail. Also, make sure the EndThreadProc
// is called just like EndThread would do. EndThreadProc should not return
// and call pthread_exit itself.
if Assigned(EndThreadProc) then
is it a normal behavior ? if yes, i m the only one to think that arc is the worse think of delphi ?