Delphi ITask

编程入门 行业动态 更新时间:2024-10-22 11:05:58
Delphi ITask - 我应该在完成后释放它吗?(Delphi ITask - should I free it after completed?)

我创建了一个ITask ( TTask.Create(...) )。 它按预期工作。 完成作业后,任务状态显示“已完成”。

我应该以某种方式释放ITask对象,还是自动释放?

I create an ITask (TTask.Create(...)). It works as expected. After it finishes the job, the task status shows me "completed".

Should I free the ITask object somehow, or it is freed automatically?

最满意答案

与在其后代中实现TInterfacedObject任何接口一样(默认), ITask是引用计数。 这意味着一旦其引用计数为零,它将自毁。 这意味着您不必手动释放它。

请参阅接口对象的内存管理 :

接口设计背后的一个概念是确保实现它们的对象的生命周期管理。 IInterface的_AddRef和_Release方法提供了实现此生命周期管理的方法。 _AddRef和_Release通过在将接口引用传递给客户端时递增对象的引用计数来跟踪对象的生命周期,并在引用计数为零时销毁该对象。


如果任务完成,也可以通过指定nil值来销毁FTask 。 这也将减少引用计数。

As with any interface that implements TInterfacedObject in their descendants (default), ITask is reference counted. This means that it will self-destruct, once its reference count is zero. This means that you don't have to free it manually.

See Memory Management of Interface Objects:

One of the concepts behind the design of interfaces is ensuring the lifetime management of the objects that implement them. The _AddRef and _Release methods of IInterface provide a way to implement this lifetime management. _AddRef and _Release track the lifetime of an object by incrementing the reference count on the object when an interface reference is passed to a client, and will destroy the object when that reference count is zero.


If the task is finished, it is also possible to destroy FTask by assigning a nil value. That will also decrement the reference count.

更多推荐

本文发布于:2023-07-16 12:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128581.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Delphi   ITask

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!