title: Closure in TTask.Run(AnonProc) not released after AnonProc has finished
body: <p>Anonymous methods in Delphi create a closure, which keeps "surrounding" local variables in context, until the anonymous method has finished. If using interface variables, then they will decrease their referenced instance not before the anonymous method has finished. So far so good.</p>
  
  <p>When using TTask.Run(AProc:TProc) with an anonymous method I would expect that the closure will be released when the associated worker thread has finished executing "AProc". This does not seem to happen though. On program termination, when the thread pool (that this TTask generated thread belongs to) gets released, you can finally see that these locally referenced instances get released - i.e. the closure gets apparently released.</p>
  
  <p>The question is if this is a feature or a bug? Or do I oversee something here?</p>
  
  <p>Below, after TTask.Run(...).wait I would expect LFoo's destructor to be called - which does not happen.</p>
  
  <pre><code>procedure Test3;
  var
    LFoo: IFoo;
  begin
    LFoo := TFoo.Create;
  
    TTask.Run(
      procedure
      begin
        Something(LFoo);
      end).Wait; // Wait for task to finish
  
     //After TTask.Run has finished, it should let go LFoo out of scope - which it does not apprently. 
  end;
  </code></pre>
  
  <p>Following is a full test case, which shows that a "simple" anonymous method works as expected (Test2), but when fed into TTask.Run it does not (Test3)</p>
  
  <pre><code>program InterfaceBug;
  
  {$APPTYPE CONSOLE}
  {$R *.res}
  
  uses
    System.Classes,
    System.SysUtils,
    System.Threading;
  
  type
  
    //Simple Interface/Class
    IFoo = interface(IInterface)
      ['{7B78D718-4BA1-44F2-86CB-DDD05EF2FC56}']
      procedure Bar;
    end;
  
    TFoo = class(TInterfacedObject, IFoo)
    public
      constructor Create;
      destructor Destroy; override;
      procedure Bar;
    end;
  
  procedure TFoo.Bar;
  begin
    Writeln('Foo.Bar');
  end;
  
  constructor TFoo.Create;
  begin
    inherited;
    Writeln('Foo.Create');
  end;
  
  destructor TFoo.Destroy;
  begin
    Writeln('Foo.Destroy');
    inherited;
  end;
  
  procedure Something(const AFoo: IFoo);
  begin
    Writeln('Something');
    AFoo.Bar;
  end;
  
  procedure Test1;
  var
    LFoo: IFoo;
  begin
    Writeln('Test1...');
    LFoo := TFoo.Create;
    Something(LFoo);
    Writeln('Test1 done.');
    //LFoo goes out od scope, and the destructor gets called
  end;
  
  procedure Test2;
  var
    LFoo: IFoo;
    LProc: TProc;
  begin
    Writeln('Test2...');
    LFoo := TFoo.Create;
    LProc := procedure
      begin
        Something(LFoo);
      end;
    LProc();
    Writeln('Test2 done.');
     //LFoo goes out od scope, and the destructor gets called
  end;
  
  procedure Test3;
  var
    LFoo: IFoo;
  begin
    Writeln('Test3...');
    LFoo := TFoo.Create;
    TTask.Run(
      procedure
      begin
        Something(LFoo);
      end).Wait; // Wait for task to finish
    //LFoo := nil;  This would call TFoo's destructor,
    //but it should get called automatically with LFoo going out of scope - which apparently does not happen!
    Writeln('Test3 done.');
  end;
  
  begin
    try
      Test1; //works
      Writeln;
      Test2; //works
      Writeln;
      Test3; //fails
      Writeln('--------');
      Writeln('Expected: Three calls of Foo.Create and three corresponding ones of Foo.Destroy');
      Writeln;
      Writeln('Actual: The the third Foo.Destroy is missing and is executed when the program terminates, i.e. when the default ThreadPool gets destroyed.');
      ReadLn;
    except
      on E: Exception do
        Writeln(E.ClassName, ': ', E.Message);
    end;
  
  end.
  </code></pre>