最简单的 COM Interop 应用程序泄漏内存 - 我做错了什么?
C#
namespace TestController
{
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IController {
void DoSomething();
}
[ComVisible(true), ClassInterface(ClassInterfaceType.None)]
public class ControllerImpl : IController {
public void DoSomething()
{
throw new NotImplementedException();
}
}
}
C++
#import "c:\prj\Controller\bin\Debug\TestController.tlb"
using namespace TestController;
int _tmain(int argc, _TCHAR* argv[])
{
IControllerPtr ctrl;
CoInitializeEx(NULL, COINIT_MULTITHREADED);
while (true) {
HRESULT hr = ctrl.CreateInstance(__uuidof(ControllerImpl));
ctrl = 0;
}
return 0;
}
大家好,
我需要从非托管代码提供对 .NET 类库的访问。作为这个主题的新手,我花了几天时间研究 COM / 互操作,然后定义并实现了一个 COM 可访问接口,进行了测试运行,一切正常,直到我注意到一些似乎是内存泄漏的东西。我隔离了有问题的语句,但仍然不知道为什么上面的代码被破坏了。
C#
namespace TestController
{
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IController {
void DoSomething();
}
[ComVisible(true), ClassInterface(ClassInterfaceType.None)]
public class ControllerImpl : IController {
public void DoSomething()
{
throw new NotImplementedException();
}
}
}
C++
#import "c:\prj\Controller\bin\Debug\TestController.tlb"
using namespace TestController;
int _tmain(int argc, _TCHAR* argv[])
{
IControllerPtr ctrl;
CoInitializeEx(NULL, COINIT_MULTITHREADED);
while (true) {
HRESULT hr = ctrl.CreateInstance(__uuidof(ControllerImpl));
ctrl = 0;
}
return 0;
}
Hi all,
I need to provide access to my .NET class library from unmanaged code. Beeing totally new to the subject, I spent several days studying the COM / interop, then defined and implemented a COM accessible interface, made a test run and everything just worked, until I noticed something that seemed as a memory leak. I isolated the offending statements, but still have no clue why is the above code broken.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是这样的情况:
http ://jpassing.com/2009/03/26/rcw-reference-counting-rules-com-reference-counting-rules/
具体来说,.NET使用RCW引用计数规则,与通常的引用不同COM 应用程序中的计数规则。
这会导致明显的泄漏,直到 .NET RCW 引用计数观察到给定对象不再使用,这需要其垃圾收集器的配合。
This is probably a case of:
http://jpassing.com/2009/03/26/rcw-reference-counting-rules-com-reference-counting-rules/
Specifically, .NET uses RCW reference counting rules, which are different to the usual reference counting rules in COM applications.
This results in apparent leaks until the .NET RCW reference counting observes that a given object is no-longer used, and this requires cooperation from its garbage collector.