使用“动态"时 RuntimeBinder 中的泄漏带有 __ComObject 的关键字 [英] Leak in RuntimeBinder when using "dynamic" keyword with __ComObject

查看:23
本文介绍了使用“动态"时 RuntimeBinder 中的泄漏带有 __ComObject 的关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道在 C# 中的 __ComObject 实例中使用dynamic"关键字时是否有办法防止 RuntimeBinder 中的内存泄漏?

Does anybody know if there is a way of preventing a memory leak in RuntimeBinder when using "dynamic" keyword with __ComObject instances in C#?

我得到以下代码:

var t = Type.GetTypeFromCLSID(new Guid("BB06C0E4-D293-4f75-8A90-CB05B6477EEE"));
while (true)
{
    dynamic o = System.Activator.CreateInstance(t);
    Marshal.ReleaseComObject(o);
}

这会泄漏 LocalVariableSymbol 类实例(以及来自 Microsoft.CSharp.RuntimeBinder.Semantics 命名空间的其他实例).

This leaks LocalVariableSymbol class instances (and other from the Microsoft.CSharp.RuntimeBinder.Semantics namespace).

用对象"替换动态",即:

Replacing "dynamic" with "object" i.e.:

    object o = System.Activator.CreateInstance(t);

修复了泄漏,但我更愿意继续使用动态(实际代码要复杂得多,并且使用了动态").

fixes the leak but I'd prefer to keep using dynamic (the actual code is much more complex and makes use of "dynamic").

我知道 RuntimeBinder 单例缓存数据,这会导致泄漏,但您知道是否有任何方法可以清理缓存等吗?

I know the RuntimeBinder singleton caches the data and this causes the leak but do you know if there's any way to cleanup the cache etc.?

非常感谢!

类似问题:

相关链接:

  • https://connect.microsoft.com/VisualStudio/feedback/details/1925659 (retired by Microsoft)
  • https://github.com/dotnet/roslyn/issues/2887

推荐答案

我的解决方案是替换:

dynamic o = System.Activator.CreateInstance(t);

与:

object o = System.Activator.CreateInstance(t);
dynamic d = o;

应用变通方法后不再发生内存泄漏.

The memory leak no longer occurs having the workaround applied.

这篇关于使用“动态"时 RuntimeBinder 中的泄漏带有 __ComObject 的关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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