- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Delphi 中的匿名方法创建一个闭包,该闭包在上下文中保持“包围”局部变量,直到匿名方法完成。如果使用接口(interface)变量,那么它们将在匿名方法完成之前减少其引用的实例。到目前为止一切顺利。
当将 TTask.Run(AProc:TProc) 与匿名方法一起使用时,我希望当关联的工作线程完成执行“AProc”时,将释放闭包。但这似乎并没有发生。在程序终止时,当线程池(此 TTask 生成的线程所属)被释放时,您最终可以看到这些本地引用的实例被释放 - 即闭包明显被释放。
问题是这是一个功能还是一个错误?或者我应该在这里监督一些事情吗?
下面,在 TTask.Run(...).wait 之后,我希望调用 LFoo 的析构函数 - 但这不会发生。
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;
以下是一个完整的测试用例,它显示“简单”匿名方法按预期工作(测试 2),但当输入 TTask.Run 时却不然(测试 3)
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.
最佳答案
我对这个错误做了更多分析,以找出 ITask
的真正原因。举行于TThreadPool.TQueueWorkerThread.Execute
如 known issue 中所述.
下面这行看似无辜的代码就是问题所在:
Item := ThreadPool.FQueue.Dequeue;
为什么会这样呢?因为TQueue<T>.Dequeue
被标记为内联,现在你必须知道编译器不会应用所谓的 return value optimization用于返回托管类型的内联函数。
这意味着编译器真正将之前的行翻译(我非常简化了)到此代码中。 tmp
是编译器生成的变量 - 它在方法的序言中在堆栈上保留空间:
tmp := ThreadPool.FQueue.Dequeue;
Item := tmp;
此变量在 end
中最终确定该方法的。您可以在那里放置一个断点,并将一个断点放入 TTask.Destroy
然后您会看到,当应用程序结束时,一旦到达方法末尾,这将触发最后一个 TTask
实例被销毁,因为保持其事件状态的临时变量正在被清除。
我使用了一些小技巧在本地解决了这个问题。我添加了这个本地过程来消除临时变量潜入TThreadPool.TQueueWorkerThread.Execute
方法:
procedure InternalDequeue(var Item: IThreadPoolWorkItem);
begin
Item := ThreadPool.FQueue.Dequeue;
end;
然后更改方法内的代码:
InternalDequeue(Item);
这仍然会导致 Dequeue
生成一个临时变量,但现在它只存在于 InternalDequeue
中方法,一旦退出就会被清除。
编辑(09.11.2017):这已在编译器的 10.2 中修复。现在,它在将临时变量分配给实际变量之后插入一个finally block ,这样临时变量就不会导致额外的引用超过其应有的时间。
关于multithreading - AnonProc 完成后,TTask.Run(AnonProc) 中的闭包未释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40653204/
Delphi 中的匿名方法创建一个闭包,该闭包在上下文中保持“包围”局部变量,直到匿名方法完成。如果使用接口(interface)变量,那么它们将在匿名方法完成之前减少其引用的实例。到目前为止一切顺利
我是一名优秀的程序员,十分优秀!