在 C# 中使用 COM IDispatch::GetTypeInfo() 时发生内存泄漏
我在 COM dll(C#、.NET Framework v2)中有以下函数:
public void Leak(object jsObject) {
Type comType;
IDispatch disp = (IDispatch)jsObject;
disp.GetTypeInfo(0, 0, out comType); // this line causes the leak
Marshal.FinalReleaseComObject(disp);
Marshal.FinalReleaseComObject(jsObject);
disp = null;
jsObject = null;
GC.Collect(); GC.WaitForPendingFinalizers();
}
当从 JScript 重复调用此函数时,它会泄漏大量内存:
var util = new ActiveXObject('MyLeakyCOM.MyLeakyCOM');
for(var i = 0; i < 1000; i++) {
util.Leak({});
}
我已经尝试使用 while(Marshal. ReleaseComObject(disp) > 0) {}
但也没有运气。
I have the following function in a COM dll (C#, .NET framework v2):
public void Leak(object jsObject) {
Type comType;
IDispatch disp = (IDispatch)jsObject;
disp.GetTypeInfo(0, 0, out comType); // this line causes the leak
Marshal.FinalReleaseComObject(disp);
Marshal.FinalReleaseComObject(jsObject);
disp = null;
jsObject = null;
GC.Collect(); GC.WaitForPendingFinalizers();
}
When calling this function repeatedly from a JScript, it leaks lots of memory:
var util = new ActiveXObject('MyLeakyCOM.MyLeakyCOM');
for(var i = 0; i < 1000; i++) {
util.Leak({});
}
I've already tried to release the object with while(Marshal.ReleaseComObject(disp) > 0) {}
but also no luck.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我是一个 C++ 人员而不是 C# 人员,但我突然意识到您还应该发布
comType
:ITypeInfo
对象是一个正确的 COM 对象,它是>AddRef
将由GetTypeInfo
的实现调用。I'm a C++ guy rather than a C# guy, but it strikes me that you should also be releasing
comType
:The
ITypeInfo
object is a proper COM object and it'sAddRef
will have been called by the implementation ofGetTypeInfo
.