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

查看:27
本文介绍了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天全站免登陆