C# + COM 互操作,确定性发布 [英] C# + COM Interop, deterministic release

查看:18
本文介绍了C# + COM 互操作,确定性发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 互操作,确定性发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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