mlabbe / nativefiledialog

A tiny, neat C library that portably invokes native file open and save dialogs.
zlib License
1.78k stars 209 forks source link

Windows CoUninitialize() should only be called if CoInitializeEx() succeeded #53

Closed btzy closed 5 years ago

btzy commented 6 years ago

Lines 369-373 of nfd_win.cpp (using devel branch):

if ( !SUCCEEDED(result))
{
    NFDi_SetError("Could not initialize COM.");
    goto end;
}

In the existing implementation, the above code is executed when CoInitializeEx() fails. end is where CoUninitialize() is called. Calling CoUninitialize() if CoInitializeEx() fails is wrong, according to the MSDN documentation:

To close the COM library gracefully on a thread, each successful call to CoInitialize or CoInitializeEx, including any call that returns S_FALSE, must be balanced by a corresponding call to CoUninitialize.

So we shouldn't be calling CoUninitialize() if it CoInitializeEx() does not succeed.

mlabbe commented 5 years ago

Fixed in f055c30.