coreaudio: Workaround for crash when disconnecting a bluetooth audio device.

Our understanding of what's going on here might be incorrect, but it seems
like we're getting this callback at a point where we shouldn't be able to,
with a device we've already closed.

If we're on the wrong track, this code should still be harmless; it just
verifies a device is still in the open list before dereferencing it.

Reference Issue #10432.
This commit is contained in:
Ryan C. Gordon
2025-01-04 00:27:33 -05:00
parent 1fa217434b
commit 20574c016a

View File

@@ -673,7 +673,21 @@ static OSStatus default_device_changed(AudioObjectID inObjectID, UInt32 inNumber
#if DEBUG_COREAUDIO #if DEBUG_COREAUDIO
printf("COREAUDIO: default device changed for SDL audio device %p!\n", this); printf("COREAUDIO: default device changed for SDL audio device %p!\n", this);
#endif #endif
/* due to a bug (?) in CoreAudio, this seems able to fire for a device pointer that's already closed, so check our list to make sure
the pointer is still valid before touching it. https://github.com/libsdl-org/SDL/issues/10432 */
if (open_devices) {
int i;
for (i = 0; i < num_open_devices; i++) {
SDL_AudioDevice *device = open_devices[i];
if (device == this) {
if (this->hidden) {
SDL_AtomicSet(&this->hidden->device_change_flag, 1); /* let the audioqueue thread pick up on this when safe to do so. */ SDL_AtomicSet(&this->hidden->device_change_flag, 1); /* let the audioqueue thread pick up on this when safe to do so. */
}
return noErr;
}
}
}
return noErr; return noErr;
} }
#endif #endif