当没有更多的引用时,接口实现形式是否会自行释放? [英] Will an interface-implementing form free itself when there are no more references to it?

查看:110
本文介绍了当没有更多的引用时,接口实现形式是否会自行释放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在 TMyForm = class(TForm,IMyInterface)等表单上实现接口,那么当没有更多接口引用时,该对象是否会自行释放?似乎没有,虽然我无法弄清楚如何计算TForm(如果有的话)。当接口引用超出范围时,我担心表单会被释放,但这似乎不会发生。

If I implement an interface on a form such as TMyForm = class(TForm, IMyInterface), will the object free itself when there are no more interface references to it? It seems not to, although I couldn't work out how TForm is reference counted (if at all). I'm concerned about the form getting freed when an interface reference goes out of scope, but this does not seem to happen.

我想这个问题有两个部分,首先是表格是否可能意外释放(真正的问题),其次是表格的引用计数。

I guess there are two parts to the question, firstly whether a form might get unexpectedly freed (the real question), and secondly how forms are reference counted.

推荐答案

TForm 派生自 TComponent ,它实现 _AddRef() _Release()禁用对自身的引用计数。因此,任何 TComponent 后代类实现的任何接口,如 TForm ,都不会释放其实现<$ c $发布接口时默认为c> TComponent 对象。

TForm derives from TComponent, which implements _AddRef() and _Release() to disable reference counting on itself. As such, any interface implemented by any TComponent descendant class, like TForm, will not free its implementing TComponent object by default when the interface is released.

但是,如果 IVCLComObject 接口被分配给 TComponent.VCLCOMObject 属性,然后 TComponent 将委托引用计数到该对象,以便如果对象的引用计数降为0,则可以释放该对象( TComponent 不会增加其 IVCLCOMObject 引用的引用计数) 。

However, if an IVCLComObject interface is assigned to the TComponent.VCLCOMObject property, then TComponent will delegate reference counting to that object, so that object can be freed if its reference count falls to 0 (TComponent does not increment the reference count of its IVCLCOMObject reference).

这对 TComponent 的所有后代都有效,除非他们通过覆盖实现自己的任何引用计数 _AddRef() _Release()手动。

This is valid for all descendants of TComponent, unless they implement any reference counting of their own by overriding _AddRef() and _Release() manually.

这篇关于当没有更多的引用时,接口实现形式是否会自行释放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆