From 3b39ed9d10274e9fff748a0e5b98aa62134a64e8 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Wed, 15 Dec 2021 14:24:00 -0800 Subject: [PATCH] fix possible race condition in OnBufferEndCallback --- gamemaker/extensions/FAudioGMS/FAudioGMS.dll | 2 +- src/FAudioGMS.c | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/gamemaker/extensions/FAudioGMS/FAudioGMS.dll b/gamemaker/extensions/FAudioGMS/FAudioGMS.dll index e3bc0f0..eb24139 100644 --- a/gamemaker/extensions/FAudioGMS/FAudioGMS.dll +++ b/gamemaker/extensions/FAudioGMS/FAudioGMS.dll @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:21c45d674003ac89a5227f8984941cbd26d358f7b089b21c5bdbe75442491acc +oid sha256:da07c6285594a2be3dd90139952d3ba038f7a9004ba4623d59d50c03cc18657a size 1522176 diff --git a/src/FAudioGMS.c b/src/FAudioGMS.c index 80cfd06..f7c144a 100644 --- a/src/FAudioGMS.c +++ b/src/FAudioGMS.c @@ -268,6 +268,8 @@ typedef struct FAudioGMS_Device IdStack effectChainIndexStack; double timestep; + + SDL_mutex *destroyMutex; } FAudioGMS_Device; static FAudioGMS_Device *device = NULL; @@ -386,9 +388,11 @@ static void FAudioGMS_INTERNAL_OnBufferEndCallback( if (instance->destroyOnFinish) { + SDL_LockMutex(device->destroyMutex); + /* this is a callback so we don't want to immediately destroy and screw up data */ instance->destroyTimerActive = 1; - instance->destroyTimer = 0; + instance->destroyTimer = 10; /* a little wiggle room */ if (instance->voice.effectChainAttached) { @@ -403,6 +407,8 @@ static void FAudioGMS_INTERNAL_OnBufferEndCallback( } } } + + SDL_UnlockMutex(device->destroyMutex); } } } @@ -542,6 +548,8 @@ void FAudioGMS_Init(double spatialDistanceScale, double timestep) device->timestep = timestep; + device->destroyMutex = SDL_CreateMutex(); + Log("FAudio initialized successfully!"); } @@ -2115,6 +2123,7 @@ void FAudioGMS_Update() FAudioGMS_INTERNAL_SoundInstance_SetVolume(instance, volume); } + SDL_LockMutex(device->destroyMutex); if (instance->destroyTimerActive) { instance->destroyTimer -= device->timestep; @@ -2124,6 +2133,7 @@ void FAudioGMS_Update() FAudioGMS_INTERNAL_SoundInstance_Destroy(instance); } } + SDL_UnlockMutex(device->destroyMutex); } } } @@ -2195,6 +2205,7 @@ void FAudioGMS_Destroy() SDL_free(device->fauxMasteringVoice.sends.pSends); FAudio_Release(device->handle); + SDL_DestroyMutex(device->destroyMutex); SDL_free(device); device = NULL;