title: How to destroy/free an anonymousThread
body: <p>On windows, to stop and destroy an anonymous thread, i simply do <strong>FmyTask.free</strong> => that will call <strong>destroy</strong> => and that inside the destroy will set <strong>terminate = true</strong> and call <strong>waitfor</strong> to wait the task is finished => and finally clean the memory used</p>
  
  <p>but on ARC, everything is different :(
  i use this code :</p>
  
  <pre><code>TMyObject
  private
    FMyTask: TThread;
  public
    destructor Destroy; override;
    procedure DoSomething; 
  end;
  
  destructor TMyObject.Destroy;
  begin
    FMyTask.free;  // &lt;&lt; will do nothing because FMyTask.refcount = 2 !! how it's possible ?
    FMyTask:= nil;
    inherited;
  end;
  
  procedure TMyObject.DoSomething;
  begin
  
    FMyTask:= Thread.CreateAnonymousThread(
      procedure
      begin
        sleep(10000000);
      end);
    FMyTask.FreeOnTerminate := False;
    FMyTask.Start;
  
  end;
  </code></pre>
  
  <p>and i do nothing else than just</p>
  
  <pre><code>MyObject := TmyObject.create;
  MyObject.DoSomething;
  MyObject.free;
  MyObject := nil;
  </code></pre>
  
  <p>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 :(</p>
  
  <p>it's sean that a reference to fMyTask is keep inside this function :</p>
  
  <pre><code>function ThreadProc(const Thread: TThread): Integer;
  var
    FreeThread: Boolean;
  {$IFDEF MACOS}
    pool: Pointer;
  {$ENDIF MACOS}
  begin
  {$IFDEF AUTOREFCOUNT}
    Thread.__ObjAddRef; // this ensures the instance remains for as long as the thread is running
  {$ENDIF}
    TThread.FCurrentThread := Thread;
  {$IF Defined(POSIX)}
    if Thread.FSuspended then
      pthread_mutex_lock(Thread.FCreateSuspendedMutex);
  {$ENDIF POSIX}
  {$IFDEF MACOS}
    // Register the auto release pool
    pool := objc_msgSend(objc_msgSend(objc_getClass('NSAutoreleasePool'),
                                      sel_getUid('alloc')), sel_getUid('init'));
  {$ENDIF MACOS}
    try
      Thread.FStarted := True;
      if not Thread.Terminated then
      try
        Thread.Execute;
      except
        Thread.FFatalException := AcquireExceptionObject;
      end;
    finally
      Result := Thread.FReturnValue;
      FreeThread := Thread.FFreeOnTerminate;
      Thread.DoTerminate;
      Thread.FFinished := True;
      SignalSyncEvent;
      if FreeThread then
      begin
        Thread.DisposeOf;
  {$IFDEF AUTOREFCOUNT}
        Thread.__ObjRelease; // This will clear the thread reference that was added by setting FreeOnTerminate.
  {$ENDIF}
      end;
  {$IFDEF AUTOREFCOUNT}
      Thread.__ObjRelease; // This will clear the thread reference we added above. This may initiate disposal.
  {$ENDIF}
  {$IFDEF USE_LIBICU}
      // Destroy Collator Cache
      ClearCollatorCache;
  {$ENDIF}
  {$IF Defined(MSWINDOWS)}
      EndThread(Result);
  {$ELSEIF Defined(POSIX)}
  {$IFDEF MACOS}
      // Last thing to do in thread is to drain the pool
      objc_msgSend(pool, sel_getUid('drain'));
  {$ENDIF MACOS}
  {$IFDEF ANDROID}
      // Detach the NativeActivity virtual machine to ensure the proper relase of JNI context attached to the current thread
      PJavaVM(System.JavaMachine)^.DetachCurrentThread(PJavaVM(System.JavaMachine));
  {$ENDIF ANDROID}
      // 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
        EndThreadProc(Result);
      pthread_exit(Result);
  {$ENDIF POSIX}
    end;
  end;
  </code></pre>
  
  <p>is it a normal behavior ? if yes, i m the only one to think that arc is the worse think of delphi ?</p>