From 8325df25aa630d800eb51986e3b25d3ef23416d9 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Thu, 24 May 2018 07:30:24 -0700 Subject: [PATCH] Fixed bug 4169 - Crash due to audio session observer race condition Jona The following explains why this bug was happening: This crash was caused because the audio session was being set as active [session setActive:YES error:&err] when the audio device was actually being CLOSED. Certain cases the audio session being set to active would fail and the method would return right away. Because of the way the error was handled we never removed the SDLInterruptionListener thus leaking it. Later when an interruption was received the THIS_ object would contain a pointer to an already released device causing the crash. The fix: When only one device remained open and it was being closed we needed to set the audio session as NOT active and completely ignore the returned error to successfully release the SDLInterruptionListener. I think the user assumed that the open_playback_devices and open_capture_devices would equal 0 when all of them where closed but the truth is that at the end of the closing process that the open devices count is decremented. --- src/audio/coreaudio/SDL_coreaudio.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/audio/coreaudio/SDL_coreaudio.m b/src/audio/coreaudio/SDL_coreaudio.m index 1e68331bf055f..59242f935a1fd 100644 --- a/src/audio/coreaudio/SDL_coreaudio.m +++ b/src/audio/coreaudio/SDL_coreaudio.m @@ -355,7 +355,7 @@ static BOOL update_audio_session(_THIS, SDL_bool open) return NO; } - if (open_playback_devices + open_capture_devices == 1) { + if (open && (open_playback_devices + open_capture_devices) == 1) { if (![session setActive:YES error:&err]) { NSString *desc = err.description; SDL_SetError("Could not activate Audio Session: %s", desc.UTF8String); @@ -392,10 +392,10 @@ static BOOL update_audio_session(_THIS, SDL_bool open) if (this->hidden->interruption_listener != NULL) { SDLInterruptionListener *listener = nil; listener = (SDLInterruptionListener *) CFBridgingRelease(this->hidden->interruption_listener); + [center removeObserver:listener]; @synchronized (listener) { listener.device = NULL; } - [center removeObserver:listener]; } } }