Skip to content

Commit

Permalink
Fix assertion when sound fails to be loaded
Browse files Browse the repository at this point in the history
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 ddnet#9431.
  • Loading branch information
Robyt3 committed Dec 28, 2024
1 parent c7a32ab commit f75acbe
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/engine/client/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f75acbe

Please sign in to comment.