问题描述
在C#中使用__ComObject实例使用dynamic关键字时,有没有人知道是否有防止RuntimeBinder中的内存泄漏的方法?我得到以下代码:
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中的其他类实例)。语义命名空间)。
用对象替换动态,即:
object o = System.Activator.CreateInstance(t);
修复漏洞,但我宁愿继续使用动态(实际代码复杂得多,使用动态)。
我知道RuntimeBinder单例缓存数据,这会导致泄漏,但是你知道是否有任何方法来清除缓存等非常感谢!
类似的问题: p>
相关链接:
我的案例中的解决方案是替换:
dynamic o = System.Activator.CreateInstance(t);
with:
code> object o = System.Activator.CreateInstance(t);
dynamic d = o;
应用了解决方法,内存泄漏不再发生。
Does anybody know if there is a way of preventing a memory leak in RuntimeBinder when using "dynamic" keyword with __ComObject instances in C#?
I got the following code:
var t = Type.GetTypeFromCLSID(new Guid("BB06C0E4-D293-4f75-8A90-CB05B6477EEE"));
while (true)
{
dynamic o = System.Activator.CreateInstance(t);
Marshal.ReleaseComObject(o);
}
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").
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.?
Many thanks!
Similar questions:
- Memory Overflow: Having an increasing number of Microsoft.CSharp.RuntimeBinder.Semantics
- Memory leak in CLR classes
Related links:
- https://connect.microsoft.com/VisualStudio/feedback/details/1925659
- https://github.com/dotnet/roslyn/issues/2887
The solution in my case was to replace:
dynamic o = System.Activator.CreateInstance(t);
with:
object o = System.Activator.CreateInstance(t);
dynamic d = o;
The memory leak no longer occurs having the workaround applied.
这篇关于使用“动态”时,RuntimeBinder中的泄漏关键字与__ComObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!