在Delphi中销毁COM对象
有一些.net程序集,通过COM在delphi中调用它。
var
intf: ITest;
...
intf:= CreateComObject(CLASS_TEST) as ITest;
...
//here comes some stuff
...
我必须做点什么来破坏它以释放内存吗?或不?
Have some .net assembly, calling it in delphi through COM.
var
intf: ITest;
...
intf:= CreateComObject(CLASS_TEST) as ITest;
...
//here comes some stuff
...
Must i do something to destruct it to free memory. Or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
COM 对象是引用计数的,当引用计数达到零时,它们会自动销毁。每当您的代码添加或删除对象的引用时,编译器都会自动添加对
_AddRef
和_Release
接口方法的调用。将引用 COM 对象的变量设置为nil
将调用_Release
(减少引用计数),如果引用计数达到零,该对象也会被释放(它不会如果引用计数不为零)。当变量超出范围时(即局部变量,当过程退出时),如果该变量引用 COM 对象(或任何引用计数的 Delphi 接口),编译器也会调用 _Release。COM objects are reference-counted, and they get automatically destroyed when the reference count reaches zero. The compiler will automatically add calls to
_AddRef
and_Release
interface methods whenever your code adds a reference to the object or removes it. Setting a variable referencing a COM object tonil
will call_Release
(decrementing the reference count), and if the reference count reaches zero the object is also freed (it won't be if the reference count is not zero). When a variable goes out of scope (i.e. a local variable, when the procedure exits), the compiler will also call_Release
if the variable references a COM object (or any reference-counted Delphi interface).所有COM 接口都必须实现
IUnknown
:IUnknown
提供两种服务。首先,QueryInterface
允许客户端获取该对象可能实现的其他接口。第二个服务是生命周期管理。每次引用 COM 对象时,您都有责任调用
_AddRef
。每次放弃对 COM 对象的引用时,您都必须调用_Release
。_AddRef
和_Release
的规范实现是让实现对象维护一个引用计数变量,该变量随着引用的获取和释放而递增和递减。如果对_Release
的调用将此引用计数设置为 0,则该对象会自行销毁。接口的 Delphi 实现代表您管理对
_AddRef
和_Release
的调用。当您分配给接口变量时,编译器会发出以下代码:_Release
(如果该变量先前确实引用过某些内容)。_AddRef
。编译器还会安排在变量离开作用域时调用
_Release
。这意味着您无需采取任何特殊操作即可确保 COM 对象被销毁。当对对象的最后一个引用离开作用域时,它们自然会被销毁。
但是,如果您确实希望提前销毁对象,则只需将
nil
分配给保存该接口的变量即可。请注意,这当然是假设没有对该接口进行其他引用。All COM interfaces must implement
IUnknown
:IUnknown
provides two services. First of all,QueryInterface
allows for clients to obtain the other interfaces that the object may implement. The second service is that of lifetime management.Every time you take a reference to a COM object it is your responsibility to call
_AddRef
. Every time you give up a reference to a COM object, you are contracted to call_Release
.The canonical implementation of
_AddRef
and_Release
is for the implementing object to maintain a reference count variable which is incremented and decremented as references are taken and released. If a call to_Release
sets this reference count to 0 then the object destroys itself.The Delphi implementation of interfaces manages the calls to
_AddRef
and_Release
on your behalf. When you assign to an interface variable the compiler emits code that:_Release
on the interface that the variable previously referred to, if indeed it did previously refer to something._AddRef
on the interface that the variable now refers to.The compiler also arranges for
_Release
to be called whenever the variable leaves scope.What this means is that you need to take no special action whatsoever to ensure that your COM objects will be destroyed. They will naturally be destroyed when the last reference to the object leaves scope.
If however, you do wish to destroy an object ahead of time, then you simply assign
nil
to the variable holding the interface. Note that this is of course presuming that no other references are held to that interface.你最好释放内存
当你不再需要它时, 。如果
intf
定义为,则最好使用
,即作为类属性。try...finally intf := nil;
块,或者在Destroy
重写方法中fIntf如果
intf
是在栈上定义的,我们会在方法结束时自动释放它。有一个隐藏的 try...finally intf := nil;编译器生成的 end 块用于释放intf
实例。You should better release the memory with
When you don't need it any more. Better with a
try...finally intf := nil;
block, or in theDestroy
overriden method ifintf
is defined asfIntf
, i.e. as a class property.If
intf
is defined on stack, it will we freed automatically at the end of the method. There is an hiddentry...finally intf := nil; end
block generated by the compiler to free theintf
instance.该对象会自动释放。但是,如果您明确想要释放 intf 变量所持有的引用,则可以将其设置为 nil。
The object is released automatically. However, if you explicitly want to release the reference that the
intf
variable holds, you can set it to nil.