CoInitialize()/ CoUninitialize()调用配对 [英] CoInitialize() / CoUninitialize() calls pairing

查看:493
本文介绍了CoInitialize()/ CoUninitialize()调用配对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用COM对象的单线程应用程序。开始时,我实际上调用了 CoInitialize(0) 两次 - 在我的代码中,第二次在应用程序的另一个子系统的代码中。第一个调用返回 S_OK ,第二个返回 S_FALSE - 正如MSDN所说。

I have a single-threaded application that uses COM objects. At the beginning I in effect call CoInitialize(0) twice - once in my code and the second time in the code of another subsystem of the application. The first call returns S_OK, the second returns S_FALSE - exactly as MSDN says.

当应用程序停止时,它调用 CoUninitialize()两次,但是在这些调用之间,它试图调用一些COM对象的方法 - 这些调用只是崩溃与访问违反,因为我认为COM对象最终确定并释放在第一次调用 CoUnitialize()。如果我删除对 CoInitialize() / CoUnitialize()的重复调用,它可以正常工作。

When the application stops it calls CoUninitialize() twice but between those calls it tries to call methods of some COM objects - those calls just crash with access violation because I suppose the COM objects are finalized and released at the first call to CoUnitialize(). If I remove the duplicating calls to CoInitialize()/CoUnitialize() it works allright.

但是为什么呢? MSDN说,我可以反复调用 CoInitialize(),并且必须只匹配具有匹配的 CoUnitialize()

But why is this? MSDN says I can call CoInitialize() repeatedly and must only pair those calls with the matching number of CoUnitialize() calls.

为什么COM对象在第一次调用 CoUninitialize()时完成。

Why are COM objects finalized at the first call to CoUninitialize().

推荐答案

听起来像你做的正确,但是,检查以确保你的couninitialize调用是在应用程序的主窗口关闭后,该窗口的消息循环已运行完成。

sounds like you're doing it correct, however, check to be sure your couninitialize calls are done after the main window for the app has closed and after the message loop for that window has run finished.

这篇关于CoInitialize()/ CoUninitialize()调用配对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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