Skip to content

Commit eb04219

Browse files
committed
audio: Enumerating audio devices will skip zombie devices still in the hash.
1 parent d06b6e4 commit eb04219

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/audio/SDL_audio.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,7 @@ static int SDLCALL RecordingAudioThread(void *devicep) // thread entry point
14151415
typedef struct CountAudioDevicesData
14161416
{
14171417
int devs_seen;
1418+
int devs_skipped;
14181419
const int num_devices;
14191420
SDL_AudioDeviceID *result;
14201421
const bool recording;
@@ -1430,7 +1431,13 @@ static bool SDLCALL CountAudioDevices(void *userdata, const SDL_HashTable *table
14301431
const bool isphysical = !!(devid & (1<<1));
14311432
if (isphysical && (devid_recording == data->recording)) {
14321433
SDL_assert(data->devs_seen < data->num_devices);
1433-
data->result[data->devs_seen++] = devid;
1434+
SDL_AudioDevice *device = (SDL_AudioDevice *) value; // this is normally risky, but we hold the device_hash_lock here.
1435+
const bool zombie = SDL_GetAtomicInt(&device->zombie) != 0;
1436+
if (zombie) {
1437+
data->devs_skipped++;
1438+
} else {
1439+
data->result[data->devs_seen++] = devid;
1440+
}
14341441
}
14351442
return true; // keep iterating.
14361443
}
@@ -1446,10 +1453,11 @@ static SDL_AudioDeviceID *GetAudioDevices(int *count, bool recording)
14461453
num_devices = SDL_GetAtomicInt(recording ? &current_audio.recording_device_count : &current_audio.playback_device_count);
14471454
result = (SDL_AudioDeviceID *) SDL_malloc((num_devices + 1) * sizeof (SDL_AudioDeviceID));
14481455
if (result) {
1449-
CountAudioDevicesData data = { 0, num_devices, result, recording };
1456+
CountAudioDevicesData data = { 0, 0, num_devices, result, recording };
14501457
SDL_IterateHashTable(current_audio.device_hash, CountAudioDevices, &data);
1451-
SDL_assert(data.devs_seen == num_devices);
1452-
result[data.devs_seen] = 0; // null-terminated.
1458+
SDL_assert((data.devs_seen + data.devs_skipped) == num_devices);
1459+
num_devices = data.devs_seen; // might be less if we skipped any.
1460+
result[num_devices] = 0; // null-terminated.
14531461
}
14541462
}
14551463
SDL_UnlockRWLock(current_audio.device_hash_lock);

0 commit comments

Comments
 (0)