views:

317

answers:

1

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.

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.

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

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

A: 

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.

Don Dickinson
Actually the first call is done while the loop is running.
sharptooth
can you move it outside the loop? that might help.
Don Dickinson
Actually that's not easy. Why does it matter in the first place?
sharptooth
it only matters because i found some documentation (an older help file) that says to do that. not sure if it will fix the problem. kind of makes sense, though.
Don Dickinson
@Don Dickinson: I don't get why this is relevant to the problem at all.
sharptooth
it is only relevant because the helpfile i read on it (an older one that came with delphi) is very specific about it. it says to call counitialize after the message loop has finished.
Don Dickinson

related questions