COM对象清除 [英] COM Object Cleanup

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

问题描述

我作为实例在C#DLL中定义在Excel中的VBA脚本的COM对象。在C#类我实例具有执行垃圾收集,特别是其他MS Office对象它引用,这是我应该看到出现,在任务管理器中消失终结。事实上,单元测试表明,一个Office程序出现时该dll被调用,并在结束时消失。

I am instantiating a COM object from a VBA script in excel as defined in a C# dll. The C# class I instantiate has a finalizer that performs garbage collection, notably other MS Office objects it references, which I should see appear and disappear in the task manager. In fact, the unit tests demonstrate that an Office process appears when the dll is called, and goes away when it is finished.

在该dll从VBA调用,我注意到进程保持开放的VBA函数结束后,即对象被调用,这使我相信,VBA不释放COM对象后notcleaned起来。

When the dll is called from the VBA, I have noticed that the processes remain open after the VBA function ends, i.e. the object is notcleaned up after being called, which leads me to believe that VBA does not release the COM object.

我的功能看起来像这样:

My function looks something like this:

Function Raz(inp As String)
 Dim o: Set o = CreateObject("foo.bar")
 o.InputFields.FirstInput = inp
 o.Update
 Raz = o.OutputFields.FirstOutput
End Function

我已经加入以下行的功能,以便年底解决问题:

I have added the following line at the end of the function in order to resolve the issue:

set o = Nothing

不过,在我的COM进程中资源仍然是我的VBA脚本的执行,完成后。我如何垃圾收集VBA我的COM对象,而无需手动创建处置电话?

However, the resources in my COM process remain after execution and completion of my VBA script. How do I garbage collect my COM objects from VBA without manually creating a dispose call?

推荐答案

COM将保持进程在内存中的而最后一个引用消失后,万一你想创建一个要求同一个进程不久的另一个对象。

COM will keep processes in memory for a while after the last reference is gone, just in case you want to create another object requiring the same process soon afterwards.

该理论认为进程创建是非常缓慢和昂贵,你可能不经意间写一个循环或东西做这样的:

The theory is that process creation is very slow and expensive, and you might inadvertently write a loop or something that does this:

 While 1=1

      Set a = CreateObject(...)
      ' Later
      Set a = Nothing

 Wend

您不希望Windows颠簸各地杀戮和在无限循环启动进程。

You wouldn't want Windows thrashing around killing and launching processes endlessly in the loop.

最后,他们走了。

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

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