SDL: audio: device thread shouldn't touch `thread_alive` after object is free'd.

From 8ac5c84ad18205bd4708e54ea642629a96f37531 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Sat, 14 Oct 2023 13:49:08 -0400
Subject: [PATCH] audio: device thread shouldn't touch `thread_alive` after
 object is free'd.

Reference Issue #8386.
---
 src/audio/SDL_audio.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index 3daa1b451e61..a14bef9cc2e9 100644
--- a/src/audio/SDL_audio.c
+++ b/src/audio/SDL_audio.c
@@ -790,15 +790,20 @@ void SDL_QuitAudio(void)
 
 void SDL_AudioThreadFinalize(SDL_AudioDevice *device)
 {
-    if (SDL_AtomicGet(&device->condemned)) {
+    const SDL_bool condemned = SDL_AtomicGet(&device->condemned) ? SDL_TRUE : SDL_FALSE;
+    if (condemned) {
         if (device->thread) {
             SDL_DetachThread(device->thread);  // no one is waiting for us, just detach ourselves.
             device->thread = NULL;
-            SDL_AtomicSet(&device->thread_alive, 0);
         }
-        DestroyPhysicalAudioDevice(device);
     }
+
+    // tell the world we're done touching this object (except if condemned, when we're the only thing that _can_ touch it).
     SDL_AtomicSet(&device->thread_alive, 0);
+
+    if (condemned) {  // nothing is coming to destroy this object, we have to do it as we exit.
+        DestroyPhysicalAudioDevice(device);
+    }
 }
 
 static void MixFloat32Audio(float *dst, const float *src, const int buffer_size)