Re: Where should I call CoUninitialize
On 1 Apr 2007 23:32:59 -0700, "Hariom Tiwari" <hariomtiwari2103@gmail.com>
wrote:
Hi,
I have declared a smart pointer of COM interface globally.
When I call CoUninitialize in ExitInstance it throws an exception.
Because it is trying to release interface pointer twice.
How can I overcome this problem?
I have to declare interface pointer globally.
You could probably make it a member of your application class. That said,
if it must be a global, then you should make your CoInitialize/Uninitialize
global as well. You can accomplish this with a class:
struct ComInitializer
{
ComInitializer()
{
CoInitialize();
}
~ComInitializer()
{
CoUninitialize();
}
};
Then, in the same file:
ComInitializer g_comInitializer;
IMyPointer g_myPointer;
This works because globals defined in the same translation unit are
constructed in the order of their definitions and destroyed in the reverse
order. Just don't do this in a DLL, because DLL globals are constructed and
destroyed in DllMain context, and these COM operations are among the many
things forbidden in DllMain.
--
Doug Harrison
Visual C++ MVP