Add destructor for SoundTrack/MusicTrack (and explicitly define move constructor to prevent double-free)

This commit is contained in:
leo60228
2020-01-30 09:49:34 -05:00
committed by Ethan Lee
parent c561cd9740
commit 2f760af439
3 changed files with 99 additions and 62 deletions

View File

@@ -24,6 +24,15 @@ MusicTrack::MusicTrack(SDL_RWops *rw)
}
}
MusicTrack::MusicTrack(MusicTrack&& moved) : m_music(move(moved.m_music)), m_isValid(move(moved.m_isValid)) {
moved.m_isValid = false;
}
MusicTrack::~MusicTrack() {
if (m_isValid) Mix_FreeMusic(m_music);
m_isValid = false;
}
SoundTrack::SoundTrack(const char* fileName)
{
sound = NULL;
@@ -38,12 +47,22 @@ SoundTrack::SoundTrack(const char* fileName)
FILESYSTEM_freeMemory(&mem);
}
if (sound == NULL)
{
if (sound == NULL) {
fprintf(stderr, "Unable to load WAV file: %s\n", Mix_GetError());
} else {
isValid = true;
}
}
SoundTrack::SoundTrack(SoundTrack&& moved) : sound(move(moved.sound)), isValid(move(moved.isValid)) {
moved.isValid = false;
}
SoundTrack::~SoundTrack() {
if (isValid) Mix_FreeChunk(sound);
isValid = false;
}
SoundSystem::SoundSystem()
{
int audio_rate = 44100;