From 54d665ca2ff9244ef4b5a6dc48424d4a3364a40c Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sun, 15 Sep 2013 01:12:30 +0300 Subject: fix music RWops memory leak. --- src/resources/sdlmusic.cpp | 19 ++++++++++++++----- src/resources/sdlmusic.h | 8 ++++++-- 2 files changed, 20 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/resources/sdlmusic.cpp b/src/resources/sdlmusic.cpp index fb31c0562..03c6922d6 100644 --- a/src/resources/sdlmusic.cpp +++ b/src/resources/sdlmusic.cpp @@ -26,28 +26,37 @@ #include "debug.h" -SDLMusic::SDLMusic(Mix_Music *const music) : +SDLMusic::SDLMusic(Mix_Music *const music, SDL_RWops *const rw) : Resource(), - mMusic(music) + mMusic(music), + mRw(rw) { } SDLMusic::~SDLMusic() { Mix_FreeMusic(mMusic); +#ifndef USE_SDL2 + if (mRw) + { + SDL_RWclose(mRw); + mRw = nullptr; + } +#endif } Resource *SDLMusic::load(SDL_RWops *const rw) { #ifdef USE_SDL2 if (Mix_Music *const music = Mix_LoadMUSType_RW(rw, MUS_OGG, 1)) + { + return new SDLMusic(music, nullptr); #else - // +++ here probably mem leak // Mix_LoadMUSType_RW was added without version changed in SDL1.2 :( if (Mix_Music *const music = Mix_LoadMUS_RW(rw)) -#endif { - return new SDLMusic(music); + return new SDLMusic(music, rw); +#endif } else { diff --git a/src/resources/sdlmusic.h b/src/resources/sdlmusic.h index 05a5528b7..23397ec61 100644 --- a/src/resources/sdlmusic.h +++ b/src/resources/sdlmusic.h @@ -29,6 +29,8 @@ #include "localconsts.h" +//struct SDL_RWops; + /** * Defines a class for loading and storing music. */ @@ -37,7 +39,8 @@ class SDLMusic final : public Resource public: SDLMusic() : Resource(), - mMusic(nullptr) + mMusic(nullptr), + mRw(nullptr) { } A_DELETE_COPY(SDLMusic) @@ -73,9 +76,10 @@ class SDLMusic final : public Resource /** * Constructor. */ - explicit SDLMusic(Mix_Music *const music); + explicit SDLMusic(Mix_Music *const music, SDL_RWops *const rw); Mix_Music *mMusic; + SDL_RWops *mRw; }; #endif // RESOURCES_SDLMUSIC_H -- cgit v1.2.3-70-g09d2