From 1ca107a057b13a73deedb741b1fcc67ec7d885d6 Mon Sep 17 00:00:00 2001 From: Marcelo Fernandez Date: Wed, 2 Aug 2017 11:45:19 -0300 Subject: [PATCH] Fix double finalisation of audio drivers --- drivers/rtaudio/audio_driver_rtaudio.cpp | 20 +++++++++++++++----- platform/windows/os_windows.cpp | 4 ---- platform/x11/os_x11.cpp | 4 ---- servers/audio_server.cpp | 5 +++-- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/drivers/rtaudio/audio_driver_rtaudio.cpp b/drivers/rtaudio/audio_driver_rtaudio.cpp index da998db66f5..3de25c32ad4 100644 --- a/drivers/rtaudio/audio_driver_rtaudio.cpp +++ b/drivers/rtaudio/audio_driver_rtaudio.cpp @@ -79,7 +79,7 @@ int AudioDriverRtAudio::callback(void *outputBuffer, void *inputBuffer, unsigned Error AudioDriverRtAudio::init() { active = false; - mutex = NULL; + mutex = Mutex::create(true); dac = memnew(RtAudio); ERR_EXPLAIN("Cannot initialize RtAudio audio driver: No devices present.") @@ -136,7 +136,6 @@ Error AudioDriverRtAudio::init() { try { dac->openStream(¶meters, NULL, RTAUDIO_SINT32, mix_rate, &buffer_size, &callback, this, &options); - mutex = Mutex::create(true); active = true; break; @@ -162,6 +161,7 @@ Error AudioDriverRtAudio::init() { try { dac->closeStream(); + active = false; } catch (RtAudioError &e) { ERR_PRINT(e.what()); ERR_FAIL_V(ERR_UNAVAILABLE); @@ -212,17 +212,27 @@ void AudioDriverRtAudio::unlock() { void AudioDriverRtAudio::finish() { - if (active && dac->isStreamOpen()) + lock(); + if (active && dac->isStreamOpen()) { dac->closeStream(); - if (mutex) + active = false; + } + unlock(); + + if (mutex) { memdelete(mutex); - if (dac) + mutex = NULL; + } + if (dac) { memdelete(dac); + dac = NULL; + } } AudioDriverRtAudio::AudioDriverRtAudio() { mutex = NULL; + dac = NULL; mix_rate = 44100; speaker_mode = SPEAKER_MODE_STEREO; } diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index da14d5c284e..a1a7e3c3214 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -1192,10 +1192,6 @@ void OS_Windows::finalize() { main_loop = NULL; - for (int i = 0; i < get_audio_driver_count(); i++) { - AudioDriverManager::get_driver(i)->finish(); - } - memdelete(joypad); memdelete(input); diff --git a/platform/x11/os_x11.cpp b/platform/x11/os_x11.cpp index 1dde328edab..ade3a0a0c5b 100644 --- a/platform/x11/os_x11.cpp +++ b/platform/x11/os_x11.cpp @@ -529,10 +529,6 @@ void OS_X11::finalize() { memdelete(main_loop); main_loop = NULL; - for (int i = 0; i < get_audio_driver_count(); i++) { - AudioDriverManager::get_driver(i)->finish(); - } - /* if (debugger_connection_console) { memdelete(debugger_connection_console); diff --git a/servers/audio_server.cpp b/servers/audio_server.cpp index 0d2550e53b3..d5f351454da 100644 --- a/servers/audio_server.cpp +++ b/servers/audio_server.cpp @@ -772,10 +772,11 @@ void AudioServer::finish() { buses.clear(); - if (AudioDriver::get_singleton()) { - AudioDriver::get_singleton()->finish(); + for (int i = 0; i < AudioDriverManager::get_driver_count(); i++) { + AudioDriverManager::get_driver(i)->finish(); } } + void AudioServer::update() { }