问题描述
我有一个托管对象,它调用 COM 服务器来分配一些内存.托管对象必须在托管对象消失之前再次调用 COM 服务器以释放该内存以避免内存泄漏.此对象实现 IDisposable
以帮助确保进行正确的内存释放 COM 调用.
I have a managed object that calls a COM server to allocate some memory. The managed object must call the COM server again to free that memory before the managed object goes away to avoid a memory leak. This object implements IDisposable
to help ensure that the correct memory-releasing COM call is made.
如果Dispose
方法被 调用,我希望对象的终结器释放内存.问题是,终结的规则是你不能访问任何引用,因为你不知道在你之前已经被 GC 和/或终结的其他对象.这使得唯一可触摸的对象状态是字段(句柄是最常见的).
In the event that the Dispose
method is not called, I would like the object's finalizer to free the memory. The trouble is, the rules of finalization is that you must not access any reference because you don't know what other objects have already been GC'd and/or finalized before you. This leaves the only touchable object state to be fields (handles being the most common).
但是调用 COM 服务器需要通过运行时可调用包装器 (RCW) 来释放内存,因为我有一个 cookie 来存储在一个字段中.从终结器调用 RCW 是否安全(是否保证此时尚未被 GC 或终结)?
But calling a COM server involves going through an runtime callable wrapper (RCW) in order to free the memory that I have a cookie to stored in a field. Is that RCW safe to call from a finalizer (is it guaranteed to have not been GC'd or finalized at this point)?
对于那些不熟悉终结的人,虽然终结器线程在运行时在托管 appdomain 的后台运行,但对于这些情况,触摸引用理论上是可以的,终结也发生在 appdomain 关闭时,并且 以任何顺序 -- 不仅仅是参考关系顺序.这限制了您可以假设从终结器中可以安全触摸的内容.任何对托管对象的引用都可能是坏的"(收集的内存),即使该引用是非空的.
For those of you not familiar with finalization, although the finalizer thread runs in the background of a managed appdomain while its running, at for those cases touching references would theoretically be OK, finalization also happens at appdomain shutdown, and in any order -- not just in reference relationship order. This limits what you can assume is safe to touch from your finalizer. Any reference to a managed object might be "bad" (collected memory) even though the reference is non-null.
更新:我刚刚试了一下,得到了这个:
Update: I just tried it and got this:
myassembly.dll 中出现System.Runtime.InteropServices.InvalidComObjectException"类型的未处理异常
An unhandled exception of type 'System.Runtime.InteropServices.InvalidComObjectException' occurred in myassembly.dll
附加信息:已与其底层 RCW 分离的 COM 对象无法使用.
Additional information: COM object that has been separated from its underlying RCW cannot be used.
推荐答案
我从 CLR 团队自己发现这确实不安全 -- 除非 你在 RCW 上分配 GCHandle 而它是这样做仍然安全(当您第一次获得 RCW 时).这确保了在需要调用它的托管对象完成之前,GC 和终结器没有总计 RCW.
I found out from the CLR team themselves that indeed it is not safe -- unless you allocate a GCHandle on the RCW while it's still safe to do so (when you first acquire the RCW). This ensures that the GC and finalizer haven't totaled the RCW before the managed object that needs to invoke it is finalized.
class MyManagedObject : IDisposable
{
private ISomeObject comServer;
private GCHandle rcwHandle;
private IServiceProvider serviceProvider;
private uint cookie;
public MyManagedObject(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
this.comServer = this. serviceProvider.GetService(/*some service*/) as ISomeObject;
this.rcwHandle = GCHandle.Alloc(this.comServer, GCHandleType.Normal);
this.cookie = comServer.GetCookie();
}
~MyManagedObject()
{
this.Dispose(false);
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// dispose owned managed objects here.
}
if (this.rcwHandle.IsAllocated)
{
// calling this RCW is safe because we have a GC handle to it.
this.comServer.ReleaseCookie(this.cookie);
// Now release the GC handle on the RCW so it can be freed as well
this.rcwHandle.Free();
}
}
}
事实证明,在我的特殊案例中,我的应用正在托管 CLR 本身.因此,它在终结器线程开始运行之前调用 mscoree!CoEEShutdownCOM,这会杀死 RCW 并导致我看到的 InvalidComObjectException
错误.
It turns out in my particular case, my app is hosting the CLR itself. Therefore, it's calling mscoree!CoEEShutdownCOM before the finalizer thread gets to run, which kills the RCW and results in the InvalidComObjectException
error I was seeing.
但在 CLR 不自行托管的正常情况下,我被告知这应该可以工作.
But in normal cases where the CLR is not hosting itself, I'm told this should work.
这篇关于从终结器调用 RCW 是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!