From f75acbe8e41b9077dca08f97d4e755f91fcfcbc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sat, 28 Dec 2024 13:54:54 +0100 Subject: [PATCH] Fix assertion when sound fails to be loaded Conditionally stop the sample in the `UnloadSample` function instead of calling the separate `Stop` function which is not supposed to be used for samples which are not loaded. The `UnloadSample` function also needs to handle samples which are not (fully) loaded to free the sample index when the sound failed to be loaded. Regression from #9431. --- src/engine/client/sound.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/engine/client/sound.cpp b/src/engine/client/sound.cpp index de1f496cfde..c5fad540ede 100644 --- a/src/engine/client/sound.cpp +++ b/src/engine/client/sound.cpp @@ -637,13 +637,25 @@ void CSound::UnloadSample(int SampleId) if(SampleId == -1) return; - Stop(SampleId); - - // Free data + dbg_assert(SampleId >= 0 && SampleId < NUM_SAMPLES, "SampleId invalid"); const CLockScope LockScope(m_SoundLock); CSample &Sample = m_aSamples[SampleId]; - free(Sample.m_pData); - Sample.m_pData = nullptr; + + if(Sample.IsLoaded()) + { + // Stop voices using this sample + for(auto &Voice : m_aVoices) + { + if(Voice.m_pSample == &Sample) + { + Voice.m_pSample = nullptr; + } + } + + // Free data + free(Sample.m_pData); + Sample.m_pData = nullptr; + } // Free slot if(Sample.m_NextFreeSampleIndex == SAMPLE_INDEX_USED)