From 510f413bb8e1edc9f44b6300171a0294699ef6ac Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sat, 15 Jun 2024 11:42:44 -0400 Subject: [PATCH] coreaudio: simplify memory leak fix. I _did_ appreciate the explanation, but it doesn't have to live in the source code; also we can just release `devuid` and then check for error with the usual macro, since SDL is done with it either way at this point. (cherry picked from commit 17af09f3a99bfea9547a6619f4da3fb0106003c2) --- src/audio/coreaudio/SDL_coreaudio.m | 31 ++--------------------------- 1 file changed, 2 insertions(+), 29 deletions(-) diff --git a/src/audio/coreaudio/SDL_coreaudio.m b/src/audio/coreaudio/SDL_coreaudio.m index 66a8c9efc6..ab03f907e0 100644 --- a/src/audio/coreaudio/SDL_coreaudio.m +++ b/src/audio/coreaudio/SDL_coreaudio.m @@ -806,35 +806,8 @@ static int assign_device_to_audioqueue(_THIS) result = AudioObjectGetPropertyData(this->hidden->deviceID, &prop, 0, NULL, &devuidsize, &devuid); CHECK_RESULT("AudioObjectGetPropertyData (kAudioDevicePropertyDeviceUID)"); result = AudioQueueSetProperty(this->hidden->audioQueue, kAudioQueueProperty_CurrentDevice, &devuid, devuidsize); - - /* - If AudioObjectGetPropertyData succeeds and AudioQueueSetProperty fails, - calling CHECK_RESULT("AudioQueueSetProperty (kAudioQueueProperty_CurrentDevice)"); - will cause a memory leak since it returns - */ - - // !!! FIXME: do we need to CFRelease(devuid) - /* Yes - CFStringRef is passed to AudioObjectGetPropertyData as an argument of type - UnsafeMutableRawPointer. As stated in the Apple Developer Documentation, - "The UnsafeMutableRawPointer type provides no automated memory management, - no type safety, and no alignment guarantees. You are responsible for - handling the life cycle of any memory you work with through - unsafe pointers, to avoid leaks or undefined behavior" - */ - - //This is the quickest fix - if (result != noErr) { - const char* msg = "AudioQueueSetProperty (kAudioQueueProperty_CurrentDevice)"; - SDL_Log("COREAUDIO: Got error %d from '%s'!\n", (int)result, msg); - CFRelease(devuid); - SDL_SetError("CoreAudio error (%s): %d", msg, (int)result); - return 0; - } - - //Release once you've finished using CFStringRef - CFRelease(devuid); - + CFRelease(devuid); /* Release devuid; we're done with it and AudioQueueSetProperty should have retained if it wants to keep it. */ + CHECK_RESULT("AudioQueueSetProperty (kAudioQueueProperty_CurrentDevice)"); return 1; } #endif