Skip to content

Commit

Permalink
win32: don't call CoUninitialize if CoInitializeEx did not succeed
Browse files Browse the repository at this point in the history
  • Loading branch information
mlabbe committed Dec 31, 2018
1 parent 23c1450 commit f055c30
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions src/nfd_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,23 +367,23 @@ nfdresult_t NFD_OpenDialog( const nfdchar_t *filterList,
nfdresult_t nfdResult = NFD_ERROR;

// Init COM library.
HRESULT result = ::CoInitializeEx(NULL,
::COINIT_APARTMENTTHREADED |
::COINIT_DISABLE_OLE1DDE );
HRESULT coResult = ::CoInitializeEx(NULL,
::COINIT_APARTMENTTHREADED |
::COINIT_DISABLE_OLE1DDE );

::IFileOpenDialog *fileOpenDialog(NULL);

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

// Create dialog
result = ::CoCreateInstance(::CLSID_FileOpenDialog, NULL,
CLSCTX_ALL, ::IID_IFileOpenDialog,
reinterpret_cast<void**>(&fileOpenDialog) );
HRESULT result = ::CoCreateInstance(::CLSID_FileOpenDialog, NULL,
CLSCTX_ALL, ::IID_IFileOpenDialog,
reinterpret_cast<void**>(&fileOpenDialog) );

if ( !SUCCEEDED(result) )
{
Expand Down Expand Up @@ -447,8 +447,11 @@ nfdresult_t NFD_OpenDialog( const nfdchar_t *filterList,
}

end:
fileOpenDialog->Release();
::CoUninitialize();
if (fileOpenDialog)
fileOpenDialog->Release();

if (SUCCEEDED(coResult))
::CoUninitialize();

return nfdResult;
}
Expand All @@ -460,10 +463,10 @@ nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList,
nfdresult_t nfdResult = NFD_ERROR;

// Init COM library.
HRESULT result = ::CoInitializeEx(NULL,
::COINIT_APARTMENTTHREADED |
::COINIT_DISABLE_OLE1DDE );
if ( !SUCCEEDED(result))
HRESULT coResult = ::CoInitializeEx(NULL,
::COINIT_APARTMENTTHREADED |
::COINIT_DISABLE_OLE1DDE );
if ( !SUCCEEDED(coResult))
{
NFDi_SetError("Could not initialize COM.");
return NFD_ERROR;
Expand All @@ -472,9 +475,9 @@ nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList,
::IFileOpenDialog *fileOpenDialog(NULL);

// Create dialog
result = ::CoCreateInstance(::CLSID_FileOpenDialog, NULL,
CLSCTX_ALL, ::IID_IFileOpenDialog,
reinterpret_cast<void**>(&fileOpenDialog) );
HRESULT result = ::CoCreateInstance(::CLSID_FileOpenDialog, NULL,
CLSCTX_ALL, ::IID_IFileOpenDialog,
reinterpret_cast<void**>(&fileOpenDialog) );

if ( !SUCCEEDED(result) )
{
Expand Down Expand Up @@ -545,7 +548,8 @@ nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList,
if ( fileOpenDialog )
fileOpenDialog->Release();

::CoUninitialize();
if (SUCCEEDED(coResult))
::CoUninitialize();

return nfdResult;
}
Expand All @@ -557,10 +561,10 @@ nfdresult_t NFD_SaveDialog( const nfdchar_t *filterList,
nfdresult_t nfdResult = NFD_ERROR;

// Init COM library.
HRESULT result = ::CoInitializeEx(NULL,
::COINIT_APARTMENTTHREADED |
::COINIT_DISABLE_OLE1DDE );
if ( !SUCCEEDED(result))
HRESULT coResult = ::CoInitializeEx(NULL,
::COINIT_APARTMENTTHREADED |
::COINIT_DISABLE_OLE1DDE );
if ( !SUCCEEDED(coResult))
{
NFDi_SetError("Could not initialize COM.");
return NFD_ERROR;
Expand All @@ -569,9 +573,9 @@ nfdresult_t NFD_SaveDialog( const nfdchar_t *filterList,
::IFileSaveDialog *fileSaveDialog(NULL);

// Create dialog
result = ::CoCreateInstance(::CLSID_FileSaveDialog, NULL,
CLSCTX_ALL, ::IID_IFileSaveDialog,
reinterpret_cast<void**>(&fileSaveDialog) );
HRESULT result = ::CoCreateInstance(::CLSID_FileSaveDialog, NULL,
CLSCTX_ALL, ::IID_IFileSaveDialog,
reinterpret_cast<void**>(&fileSaveDialog) );

if ( !SUCCEEDED(result) )
{
Expand Down Expand Up @@ -638,7 +642,9 @@ nfdresult_t NFD_SaveDialog( const nfdchar_t *filterList,
end:
if ( fileSaveDialog )
fileSaveDialog->Release();
::CoUninitialize();

if (SUCCEEDED(coResult))
::CoUninitialize();

return nfdResult;
}
Expand Down

0 comments on commit f055c30

Please sign in to comment.