COM 对象通常具有确定性破坏:它们在最后一个引用被释放时被释放.
COM objects usually have deterministic destruction: they are freed when the last reference is released.
在 C# - COM 互操作中如何处理?这些类没有实现 IDisposable
,所以我看不到触发显式 IUnknown::Release 的方法.
How is this handled in C# - COM Interop? The classes don't implement IDisposable
, so I see no way to trigger an explicit IUnknown::Release.
一个偶然的测试表明,未引用的 COM 对象被延迟收集(即垃圾收集器正在触发释放).对于需要积极释放的 OCM 对象,我该怎么办?(例如,持有大量或共享的关键资源)?
A casual test shows that unreferenced COM objects get collected lazily (i.e. the garbage collector is triggering the release). What should I do for OCM objects that need to be released aggresively? (e.g. holding large or shared critical ressources)?
原始问题:我们有一个 C# 应用程序大量使用 COM 库,并且它正在疯狂地泄漏.似乎问题在于 C++ 和 C# 代码之间"(我们可以访问两者),但我们无法确定.
Original problem: We have a C# application heavily using a COM library, and it is leaking like mad. It seems that the problems is "between" the C++ and the C# code (we have access to both), but we can't nail it down.
您可以使用 System.Runtime.InteropServices.Marshal 类来操作 COM 互操作引用.具体来说,您可能想看看 Marshal.释放ComObject.
You can manipulate COM interop references using the System.Runtime.InteropServices.Marshal class. Specifically you may want to have a look at Marshal.ReleaseComObject.
这篇关于C# + COM 互操作,确定性发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!