Skip to content

Commit

Permalink
wasapi: Handle disconnected devices that get reconnected.
Browse files Browse the repository at this point in the history
The device ID strings don't change between connects, so we need to move the
old device object out of the way if it still exists as a zombie, and let
the reconnected device get itself a fresh object.
  • Loading branch information
icculus authored and pull[bot] committed Nov 16, 2023
1 parent 2cf3f14 commit 3179654
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/core/windows/SDL_immdevice.c
Expand Up @@ -133,6 +133,18 @@ static SDL_AudioDevice *SDL_IMMDevice_Add(const SDL_bool iscapture, const char *

// see if we already have this one first.
SDL_AudioDevice *device = SDL_IMMDevice_FindByDevID(devid);
if (device) {
if (SDL_AtomicGet(&device->zombie)) {
// whoa, it came back! This can happen if you unplug and replug USB headphones while we're still keeping the SDL object alive.
// Kill this device's IMMDevice id; the device will go away when the app closes it, or maybe a new default device is chosen
// (possibly this reconnected device), so we just want to make sure IMMDevice doesn't try to find the old device by the existing ID string.
SDL_IMMDevice_HandleData *handle = (SDL_IMMDevice_HandleData *) device->handle;
SDL_free(handle->immdevice_id);
handle->immdevice_id = NULL;
device = NULL; // add a new device, below.
}
}

if (!device) {
// handle is freed by SDL_IMMDevice_FreeDeviceHandle!
SDL_IMMDevice_HandleData *handle = SDL_malloc(sizeof(SDL_IMMDevice_HandleData));
Expand Down

0 comments on commit 3179654

Please sign in to comment.