From 8cdbc28455694c2ad8fb392e622aca8bab0f7237 Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Tue, 8 Mar 2022 00:00:34 -0800 Subject: [PATCH] Core Audio Output: Add extra safety checks Add safety check to check if a device is actually alive when enumerating it, and also add nil pointer checks for the device name before trying to CFRelease it. Fixes a rare crash on device add/remove cycle, such as Bluetooth headphones. Signed-off-by: Christopher Snowhill --- Audio/Output/OutputCoreAudio.m | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Audio/Output/OutputCoreAudio.m b/Audio/Output/OutputCoreAudio.m index 5e0281548..017290590 100644 --- a/Audio/Output/OutputCoreAudio.m +++ b/Audio/Output/OutputCoreAudio.m @@ -472,6 +472,12 @@ default_device_changed(AudioObjectID inObjectID, UInt32 inNumberAddresses, const theAddress.mScope = kAudioDevicePropertyScopeOutput; for(UInt32 i = 0; i < nDevices; ++i) { + UInt32 isAlive = 0; + propsize = sizeof(isAlive); + theAddress.mSelector = kAudioDevicePropertyDeviceIsAlive; + __Verify_noErr(AudioObjectGetPropertyData(devids[i], &theAddress, 0, NULL, &propsize, &isAlive)); + if(!isAlive) continue; + CFStringRef name = NULL; propsize = sizeof(name); theAddress.mSelector = kAudioDevicePropertyDeviceNameCFString; @@ -482,7 +488,7 @@ default_device_changed(AudioObjectID inObjectID, UInt32 inNumberAddresses, const __Verify_noErr(AudioObjectGetPropertyDataSize(devids[i], &theAddress, 0, NULL, &propsize)); if(propsize < sizeof(UInt32)) { - CFRelease(name); + if(name) CFRelease(name); continue; } @@ -492,7 +498,7 @@ default_device_changed(AudioObjectID inObjectID, UInt32 inNumberAddresses, const free(bufferList); if(!bufferCount) { - CFRelease(name); + if(name) CFRelease(name); continue; }