COM:我可以在不调用Release的情况下调用CoUninitialize吗?

发布于 2024-08-05 01:54:18 字数 114 浏览 12 评论 0原文

我有一个疑问。我初始化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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

家住魔仙堡 2024-08-12 01:54:18

来自 MSDN:

http://msdn.microsoft.com /en-us/library/ms688715%28VS.85%29.aspx

应该调用 CoUninitialize
应用程序关闭,作为最后一次调用
之后对 COM 库进行了
应用程序隐藏其主窗口和
落入其主消息循环。
如果存在开放式对话
剩下的,CoUninitialize 启动一个
模态消息循环并调度任何
来自容器的待处理消息
或此 COM 应用程序的服务器。经过
发送消息,
CoUninitialize 确保
应用程序之前没有退出
接收所有待处理的消息。
非 COM 消息将被丢弃。

您应该只在关闭时调用 CoUninitialize,到那时即使内存泄漏也没关系。

From MSDN:

http://msdn.microsoft.com/en-us/library/ms688715%28VS.85%29.aspx

CoUninitialize should be called on
application shutdown, as the last call
made to the COM library after the
application hides its main windows and
falls through its main message loop.
If there are open conversations
remaining, CoUninitialize starts a
modal message loop and dispatches any
pending messages from the containers
or server for this COM application. By
dispatching the messages,
CoUninitialize ensures that the
application does not quit before
receiving all of its pending messages.
Non-COM messages are discarded.

You should only ever call CoUninitialize on shutdown and by that time it doesn't matter if you have a memory leak.

腹黑女流氓 2024-08-12 01:54:18

无论您是否取消初始化 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.

﹏半生如梦愿梦如真 2024-08-12 01:54:18

您不应该使用 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.

萌逼全场 2024-08-12 01:54:18

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文