Merge pull request #75881 from bruvzg/win_coinit

[Windows] Cleanup COM library initialization/uninitialization.
This commit is contained in:
Rémi Verschelde 2023-04-18 23:40:06 +02:00
commit c2edf14faf
No known key found for this signature in database
GPG key ID: C3336907360768E1
4 changed files with 6 additions and 9 deletions

View file

@ -206,8 +206,6 @@ Error AudioDriverWASAPI::audio_device_init(AudioDeviceWASAPI *p_device, bool p_i
IMMDeviceEnumerator *enumerator = nullptr; IMMDeviceEnumerator *enumerator = nullptr;
IMMDevice *output_device = nullptr; IMMDevice *output_device = nullptr;
CoInitialize(nullptr);
HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator); HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator);
ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
@ -582,8 +580,6 @@ PackedStringArray AudioDriverWASAPI::audio_device_get_list(bool p_input) {
list.push_back(String("Default")); list.push_back(String("Default"));
CoInitialize(nullptr);
HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator); HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator);
ERR_FAIL_COND_V(hr != S_OK, PackedStringArray()); ERR_FAIL_COND_V(hr != S_OK, PackedStringArray());
@ -702,6 +698,8 @@ void AudioDriverWASAPI::write_sample(WORD format_tag, int bits_per_sample, BYTE
} }
void AudioDriverWASAPI::thread_func(void *p_udata) { void AudioDriverWASAPI::thread_func(void *p_udata) {
CoInitializeEx(nullptr, COINIT_MULTITHREADED);
AudioDriverWASAPI *ad = static_cast<AudioDriverWASAPI *>(p_udata); AudioDriverWASAPI *ad = static_cast<AudioDriverWASAPI *>(p_udata);
uint32_t avail_frames = 0; uint32_t avail_frames = 0;
uint32_t write_ofs = 0; uint32_t write_ofs = 0;
@ -908,6 +906,7 @@ void AudioDriverWASAPI::thread_func(void *p_udata) {
OS::get_singleton()->delay_usec(1000); OS::get_singleton()->delay_usec(1000);
} }
} }
CoUninitialize();
} }
void AudioDriverWASAPI::start() { void AudioDriverWASAPI::start() {

View file

@ -4406,5 +4406,4 @@ DisplayServerWindows::~DisplayServerWindows() {
if (tts) { if (tts) {
memdelete(tts); memdelete(tts);
} }
CoUninitialize();
} }

View file

@ -201,7 +201,6 @@ void OS_Windows::initialize() {
IPUnix::make_default(); IPUnix::make_default();
main_loop = nullptr; main_loop = nullptr;
CoInitialize(nullptr);
HRESULT hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), reinterpret_cast<IUnknown **>(&dwrite_factory)); HRESULT hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), reinterpret_cast<IUnknown **>(&dwrite_factory));
if (SUCCEEDED(hr)) { if (SUCCEEDED(hr)) {
hr = dwrite_factory->GetSystemFontCollection(&font_collection, false); hr = dwrite_factory->GetSystemFontCollection(&font_collection, false);
@ -468,8 +467,6 @@ Vector<String> OS_Windows::get_video_adapter_driver_info() const {
return Vector<String>(); return Vector<String>();
} }
CoInitialize(nullptr);
HRESULT hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, uuid, (LPVOID *)&wbemLocator); HRESULT hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, uuid, (LPVOID *)&wbemLocator);
if (hr != S_OK) { if (hr != S_OK) {
return Vector<String>(); return Vector<String>();
@ -1600,6 +1597,8 @@ Error OS_Windows::move_to_trash(const String &p_path) {
OS_Windows::OS_Windows(HINSTANCE _hInstance) { OS_Windows::OS_Windows(HINSTANCE _hInstance) {
hInstance = _hInstance; hInstance = _hInstance;
CoInitializeEx(nullptr, COINIT_MULTITHREADED);
#ifdef WASAPI_ENABLED #ifdef WASAPI_ENABLED
AudioDriverManager::add_driver(&driver_wasapi); AudioDriverManager::add_driver(&driver_wasapi);
#endif #endif
@ -1627,4 +1626,5 @@ OS_Windows::OS_Windows(HINSTANCE _hInstance) {
} }
OS_Windows::~OS_Windows() { OS_Windows::~OS_Windows() {
CoUninitialize();
} }

View file

@ -251,7 +251,6 @@ TTS_Windows *TTS_Windows::get_singleton() {
TTS_Windows::TTS_Windows() { TTS_Windows::TTS_Windows() {
singleton = this; singleton = this;
CoInitialize(nullptr);
if (SUCCEEDED(CoCreateInstance(CLSID_SpVoice, nullptr, CLSCTX_ALL, IID_ISpVoice, (void **)&synth))) { if (SUCCEEDED(CoCreateInstance(CLSID_SpVoice, nullptr, CLSCTX_ALL, IID_ISpVoice, (void **)&synth))) {
ULONGLONG event_mask = SPFEI(SPEI_END_INPUT_STREAM) | SPFEI(SPEI_START_INPUT_STREAM) | SPFEI(SPEI_WORD_BOUNDARY); ULONGLONG event_mask = SPFEI(SPEI_END_INPUT_STREAM) | SPFEI(SPEI_START_INPUT_STREAM) | SPFEI(SPEI_WORD_BOUNDARY);