为什么我的COM工厂在程序生存期间永远不会释放? [英] Why is my COM factory never released during the program lifetime?

查看:211
本文介绍了为什么我的COM工厂在程序生存期间永远不会释放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本地C + ATL in-proc COM服务器。单独的测试程序

I have a native C++ ATL in-proc COM server. A separate test program


  • 调用 CoInitialize()

  • 调用 CoCreateInstance(),然后

  • c>,然后调用 CoUnitialize()并退出。

  • calls CoInitialize(),
  • calls CoCreateInstance(), then
  • calls Release() on the pointer,
  • then calls CoUnitialize() and exits.

如果我在Visual C ++调试器下运行测试程序,调试CRT报告一个内存泄漏,每次分配数量相同。

If I run the test program under Visual C++ debugger the debug CRT reports a single memory leak and each time the allocation number is the same.

我使用分配挂钩并发现没有返回到堆的对象是类工厂对象。

I used an allocation hook and found out that the object not being returned to the heap is the class factory object.

因此基本上会发生以下情况:

So basically the following happens:


  • 程序调用 CoCreateInstance()

  • COM内部调用 DllGetClassObject()

  • ATL实例化工厂并将所有权传递给调用者(COM内部)

然后工厂永远不会发布 - 我看不到足够的调用 Release()的类工厂。

and then the factory is never released - I don't see enough calls to Release() of the class factory.

发生了什么?这是COM运行时的缺陷?

What is happening? Is that a defect in COM runtime?

推荐答案

结果是ATL实现的一个问题。

Turns out it's a problem of ATL implementation.

服务器使用全局 CComModule 类实例。当 CComModule :: DllClassObject()被调用时,它创建一个类工厂实例并将其缓存在 CComModule 对象。因此,事实上 CComModule 对象拥有类工厂。当 CComModule 析构函数运行时,它不会释放缓存的类工厂。

The server uses a global CComModule class instance. When CComModule::DllClassObject() is called it creates a class factory instance and caches it in the map referenced by the CComModule object. So in fact CComModule object owns the class factory. When CComModule destructor runs it doesn't release cached class factories.

为了释放所有缓存的类工厂 CComModule :: Term()方法应在服务器卸载之前调用。 IMO实现这个的最干净的方法是从派生类中的 CComModule 派生并调用 CComModule :: Term()析构函数。

In order to release all cached class factories CComModule::Term() method should be called before the server is unloaded. IMO the cleanest way to achieve this is to derive from CComModule and call CComModule::Term() in the derived class destructor.

这篇关于为什么我的COM工厂在程序生存期间永远不会释放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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