Re: CoInitialize/CoUninitialize
* Igor Tandetnik:
"Alf P. Steinbach" <alfps@start.no> wrote in message
news:3qednWbwtcR8IvXVnZ2dnUVZ_ojinZ2d@posted.comnet
Here is bad code, relying on local COM initialization:
void doAnHTMLDialog()
{
if( !SUCCEEDED( CoInitialize() ) )
{
throwX( "Unable to initalize COM" );
}
// Do HTML dialog using COM-based IE machinery, then
CoUninitialize();
// Dontcha know, there might still be some thread using COM!
Splat! }
CoInitialize/CoUninitialize are per thread. Calling CoUninitialize in
one thread cannot affect other threads.
You'd think so, wouldn't you? :-)
But reality is a bit different.
In particular, there's much messaging between threads, and just to make the
point, the above is an actual case, not some hypothetical silly-example.
While I agree it's not the best idea, I don't see the horrible problems
in the code above you seem to ascribe to it. The code should work as
written.
I totally agree, it should. Ideally. Reality is that it doesn't. Or didn't.
Perhaps Microsoft has now fixed all their libraries (I doubt it, however).
Here is less bad code, which might even be counted as good if one
ignores exception safety aspects and lack of abstraction and
reusability:
void doAnHTMLDialog()
{
HRESULT const initResult = CoInitialize();
if( FAILED( initResult ) )
{
throwX( "Unable to initalize COM" );
}
else if( initResult != S_OK )
{
CoUninitialize();
throwX( "COM was not initialized before calling
doAnHTMLDialog." ); }
CoUninitialize(); // "Undo" the checking call of
CoInitialize.
// Do HTML dialog using COM-based IE machinery.
}
If you are willing to do the complete CoInitialize / CoUninitialize
dance just for checking, why not do it for real as in the first example,
and actually use the freshly initialized apartment
Because that doesn't work in general, but might work in particular cases (it's
like C++ Undefined Behavior), thus leading someone -- e.g. you! :-) -- to
use it, and sometime in the future get some very difficult-to-find bug.
(since you have
already invoked the overhead anyway)?
I think the overhead is there only in the unreliable case where CoInitialize
actually initializes. For a secondary or third call I imagine the overhead is
miniscule, irrelevant. But anyway this is about correctness, not speed: first
make your code correct, then, if needed, faster or whatever.
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?