fix a crash when freeing dynamically loaded sounds

This commit is contained in:
Tim Felgentreff 2022-05-22 13:59:05 +02:00
parent d6fe1ca22e
commit 52723290be
3 changed files with 19 additions and 3 deletions

View file

@ -71,6 +71,7 @@ extern bool SampleIsPlaying(Mix_Chunk *sample);
extern Mix_Music *LoadMusic(const std::string &name);
/// Load a sample
extern Mix_Chunk *LoadSample(const std::string &name);
extern void FreeSample(Mix_Chunk *sample);
/// Play a sample
extern int PlaySample(Mix_Chunk *sample, Origin *origin = NULL);
/// Play a sound file

View file

@ -411,7 +411,7 @@ static void PlaySoundFileCallback(int channel)
}
Mix_Chunk *sample = GetChannelSample(channel);
if (sample) {
Mix_FreeChunk(sample);
FreeSample(sample);
}
}
@ -590,13 +590,13 @@ CSound::~CSound()
{
if (this->Number == ONE_SOUND) {
if (Sound.OneSound) {
Mix_FreeChunk(Sound.OneSound);
FreeSample(Sound.OneSound);
}
} else if (this->Number == TWO_GROUPS) {
} else {
for (int i = 0; i < this->Number; ++i) {
if (this->Sound.OneGroup[i]) {
Mix_FreeChunk(this->Sound.OneGroup[i]);
FreeSample(this->Sound.OneGroup[i]);
}
this->Sound.OneGroup[i] = NULL;
}

View file

@ -444,6 +444,21 @@ Mix_Chunk *LoadSample(const std::string &name)
return sample;
}
/**
* Free a sample loaded with LoadSample.
*
* @param sample
*/
void FreeSample(Mix_Chunk *sample)
{
#ifdef DYNAMIC_LOAD
if (sample->allocated == 0xcafebeef) {
return;
}
#endif
Mix_FreeChunk(sample);
}
/**
** Play a sound sample
**