什么时候我需要调用ReleaseComObject的? [英] When do I need to call ReleaseComObject?



在一个Microsoft Office的AddIn我们传递事件的COM对象。举一个特定的情况下,当Word打开,我们被称为,并通过一个Document对象的文档。所以,当我们需要调用对Marshal.ReleaseComObject()?

In a Microsoft Office AddIn we are passed COM objects in events. To take a specific case, when Word opens a document we are called and passed a Document object. So when do we need to call Marshal.ReleaseComObject()?

  1. 如果我们访问的文档对象我们需要调用版本就可以了?或者,我们可以使用字已经访问它,将它清理干净?
  2. 如果我们访问Document.Name这给了我们一个字符串。作为一个字符串不是一个COM对象,我们并不需要清理一下 - 正确
  3. 如果我们访问返回一个包装COM对象(这是一个由成员方法/函数返回的任何类)一类的任何成员,我们确实需要调用释放上 - 正确


And what happens if we miss a release? Any COM object we hold onto for an indeterminate amount of time we wrap in a class of ours with IDisposable implemented. We call Dispose() when we're done. But some of the code handling this is complex and I'm guessing we have an occasional case where we're not calling Dispose.

是我们最好有一个终结,然后增加了开销,这些对象的每一个实例(很多!)?或者我们更好地与少数从未被发布的Word COM对象?

Are we better off having a finalizer which then adds overhead for every single instance of these objects (a lot!)? Or are we better off with a small number of Word COM objects that are never released?

谢谢 - 戴夫



In short, every reference you make to a COM object must be released. If you don't, the process will stay in memory.


That excludes value types (strings, etc.) and child COM objects you have not explicitly referenced.

一个例外可能会传递给你作为事件参数的COM对象。我不认为你需要释放他们,但我不能肯定。然而,一个快速测试应确认。 (有,无释放COM对象试试你的加载项。看看外接开始表现得滑稽或者任何相关进程离开后的应用程序被关闭运行。)

One exception may be COM objects passed to you as event parameters. I don't think you need to release them but I'm not certain. However a quick test should confirm that. (Try your add-in with and without releasing the COM object. See if the add-in begins to behave funny or if any related processes are left running after the app is closed.)


  1. 如果我们获得我们需要调用释放就可以了Document对象?或者,我们可以使用字已经访问它,将它清理干净的 - 你必须释放它

  1. If we access the Document object do we need to call release on it? Or can we assume Word has already accessed it and will clean it up? -- You must release it.

如果我们访问Document.Name这给了我们一个字符串。作为一个字符串不是一个COM对象,我们并不需要清理一下 - 正确的? - 你并不需要清理值类型

If we access Document.Name that gives us a string. As a string is not a COM object we do not need to clean that up - correct? -- You don't need to clean up value types.

但是,如果我们访问返回一个包装COM对象(这是任何类返回一个成员方法/功能)一类的任何成员,我们确实需要调用释放上 - 是否正确? 的 - 你并不需要释放它,如果你还没有明确的引用它。 (有一些例外,例如,在此我的问题我发现,一个特定的COM方法实例化一个COM对象,从来没有释放它,因为我有过的方法实现我唯一的选择是避免使用该方法无法控制的。)

But if we access any member that returns a class that is wrapping a COM object (which is any class returned by a member method/function), we do need to call release on that - correct? -- You don't need to release it if you haven't explicitly referenced it. (There are some exceptions. For example in this question of mine I discovered that a particular COM method was instantiating a COM object and never releasing it. Since I have no control over the method implementation my only choice was to avoid using the method.)

而发生什么,如果我们错过了一个发布的 - 这个过程(WINWORD.EXE,EXCEL.EXE等)将保持在内存中。随着时间的推移这些未终止进程会吃掉所有机器上的可用存储空间。

And what happens if we miss a release? -- The process (winword.exe, excel.exe, etc.) will remain in memory. Over time all of these unterminated processes will eat up all of the available memory on the machine.

是我们最好有一个终结,然后增加了开销,这些对象的每一个实例(很多!)? ?还是我们最好使用少量的Word COM对象的那些从未发布的 - 你最好的终结。务必释放您的COM对象!我发现,这里列出步骤是最有效的。 (这些步骤使用FinalReleaseComObject这是pferred超过ReleaseComObject的$ P $)。我也建议不要杀局过程中,以替代不释放COM。随着时间的推移,这将导致与Office互操作性问题(这是我从来没有完全理解)。

Are we better off having a finalizer which then adds overhead for every single instance of these objects (a lot!)? Or are we better off with a small number of Word COM objects that are never released? -- You're better off with the finalizer. Always release your COM objects! I've found that the steps outlined here are the most effective. (These steps use FinalReleaseComObject which is preferred over ReleaseComObject.) I also advise against killing the Office process as an alternative to not releasing COM. Over time this will cause issues with the Office interop (which I've never fully understood).


登录 关闭
发送“验证码”获取 | 15天全站免登陆