已从其基础RCW分开COM对象,不能使用 [英] COM object that has been separated from its underlying RCW cannot be used

查看:578
本文介绍了已从其基础RCW分开COM对象,不能使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些COM组件这是我从一些C#DLL调用。

I have some COM component which I call from some c# dll.

我也有使用该.dll文件WinForms应用程序。

I also have a winforms app that uses that .dll.

当我关闭该应用程序我得到这个异​​常:

When I close the app I get this exception:

这已经分居COM对象
  从它的基础RCW不能
  用了。

COM object that has been separated from its underlying RCW cannot be used.

堆栈跟踪显示此异常来自于该.dll析构函数。我实现了这个析构函数来调用COM一些清理方法。

The stack trace shows this exception comes from a destructor in the .dll. I implemented this destructor to call some cleanup method in the COM.

为什么会出现这种情况?它是如何最好地解决呢?

Why does this happen? How is it best to solve it?

推荐答案

的问题说明如下:

它是安全的从调用RCW终结?

在这里:

发布的Excel对象在析构函数

麻烦的是,不仅是时间何时这些对象
  要成为垃圾回收不确定的,但为了在其中
  终结被称为也是不确定的。在这种情况下,一个
  运行时可调用包装也有一个终结,它调用
  Marshal.FinalReleaseComObject上本身,其具有的结果
  递减的围栏,这样的COM端的引用计数
  这个COM对象可以被释放。但是,由于顺序的
  终结被称为是不确定的,它很可能是
  终结了,你的对象引用将触发COM对象
  之前终结你的对象。所以内code你
  终结可能有时工作,但,大多数时候,一个或多个的
  运行时可调用包装你的对象引用,将会产生
  已经有他们所谓的终结和底层的COM对象会
  已被释放之前,你得到终结执行其code。

The trouble is that not only is the timing as to when these objects are to be garbage collected uncertain, but the order in which the finalizers are called is also nondeterministic. In this case, a Runtime Callable Wrapper also has a finalizer, which calls Marshal.FinalReleaseComObject on itself, which has the result of decrementing the reference count on the COM side of the fence so that this COM object can be released. But since the order in which the finalizers are called is uncertain, it is very possible that the finalizers for the COM objects that your object references will fire before the finalizer for your object. So the code within your finalizer could work sometimes, but, most of the time, one or more of the Runtime Callable Wrappers that your object references will have already had their finalizers called and the underlying COM object will have been released before your finalizer gets to execute its code.

这篇关于已从其基础RCW分开COM对象,不能使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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