在Delphi中销毁COM对象 [英] Destroying COM object in Delphi

查看:397
本文介绍了在Delphi中销毁COM对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些.net程序集,通过COM在delphi中调用它。

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?

推荐答案

您应该使用

       intf := nil;

当你不再需要它。更好地用 try ... finally intf:= nil; 块,或者在 Destroy overriden方法if intf 定义为 fIntf ,即作为类属性。

When you don't need it any more. Better with a try...finally intf := nil; block, or in the Destroy overriden method if intf is defined as fIntf, i.e. as a class property.

如果 intf 在堆栈上定义,它将在方法结束时自动释放。有一个隐藏的 try ... finally intf:= nil;结束块由编译器生成以释放 intf 实例。

If intf is defined on stack, it will we freed automatically at the end of the method. There is an hidden try...finally intf := nil; end block generated by the compiler to free the intf instance.

这篇关于在Delphi中销毁COM对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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