COM:我可以在不调用Release的情况下调用CoUninitialize吗?
我有一个疑问。我初始化COM,执行CoCreateInstance并使用一些接口。我可以调用CoUninitialize而不调用Release吗?它会导致任何内存/资源泄漏吗?
提前致谢, -玛尼。
I have a doubt. I initialize COM, do CoCreateInstance and use some interfaces.Can I call CoUninitialize without calling Release? Does it cause any memory/resource leak?
Thanks in Advance,
-Mani.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
来自 MSDN:
http://msdn.microsoft.com /en-us/library/ms688715%28VS.85%29.aspx
您应该只在关闭时调用 CoUninitialize,到那时即使内存泄漏也没关系。
From MSDN:
http://msdn.microsoft.com/en-us/library/ms688715%28VS.85%29.aspx
You should only ever call CoUninitialize on shutdown and by that time it doesn't matter if you have a memory leak.
无论您是否取消初始化 COM,省略对 Release 的调用都会导致对象在服务器端保持活动状态,可能会无缘无故地保持整个服务器正常运行(如果不作为服务运行)。换句话说,服务器端会出现内存泄漏,只能通过重新启动 COM 服务器来消除。
我记得当我第一次开始使用 COM 时也问过类似的问题。我正在开发的客户端使用许多线程,并且我尝试为每个线程完成的不同任务重用接口。这使得管理接口缓存变得非常困难。最终,没有捷径可走。除非您使用 MTA、GIT 或接口封送,否则创建接口的线程也必须释放它。
为了让您更轻松,请尝试使用 CComPtr管理您创建的界面。与常规指针一样,使用智能指针有时可以让您的生活变得更加轻松。
Regardless of whether you uninitialize COM or not, omitting calls to Release will cause objects to stay alive on the server side, possibly keeping the whole server up for no reason (if not running as a service). In other words, you'll have a memory leak on the server side, which can only be eliminated by restarting the COM server.
I remember asking similar questions when I first started using COM. The client I was working on was using many threads, and I was trying to reuse interfaces for different tasks done by each thread. This made managing the interfaces cache quite difficult. Eventually, there were no shortcuts. Unless you're using MTA, GIT or interface marshaling, the thread that created the interface has to release it as well.
To make it easier on you, try using CComPtr to manage the interfaces you create. As with regular pointers, using smart pointer can sometimes make life much easier for you.
您不应该使用 CoUninitialize() 来代替调用对象的 IUnknown::Release() - 这些是完全不同的函数。
IUnknown::Release()
只会减少对象的引用计数,并可能导致其销毁。如果没有使用编组,则此调用将直接通过 vtable 完成(控制直接传递到 COM 服务器代码中),并且 COM 子系统甚至不会为此执行任何操作。CoUninitialize() 将为调用线程释放 COM 相关资源,我猜这些资源是与编组相关的对象。如果不使用编组,对象将保持未释放状态,因为只有您的代码知道它们。
这就是为什么您不应该使用其中一种来代替另一种。
You should not use CoUninitialize() instead of calling
IUnknown::Release()
for your objects - those are entrirely different functions.IUnknown::Release()
will just decrease a reference count for the object and possibly cause its destruction. In case no marshalling is used this call is done drectly throught the vtable (control is passed directly into the COM server code) and COM subsystem doesn't even do anything for that.CoUninitialize() will free COM-related resource for the calling thread which I guess are marshalling-related objects. In case no marshalling is used the objects will remain unreleased since only your code knows about them.
That's why you should not use one instead of another.
AFAIK,CoUninitialize“应该”释放当前线程上使用的所有 COM 资源。但我不会依赖它。我宁愿确保在调用 uninitialise 之前释放所有内容。
AFAIK, CoUninitialize is "supposed" to free all COM resources in use on the current thread. I wouldn't rely on it though. I'd rather make sure I Release everything prior to calling uninitialise.