1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00

Fix memory leak

SDL_RWFromMem does not take the ownership of the passed memory, so we
have to free it ourselves.
This commit is contained in:
Krzesimir Nowak 2016-08-30 23:04:16 +02:00
parent 437eadf1ed
commit 46bd1c40cc
2 changed files with 7 additions and 6 deletions

View File

@ -128,8 +128,8 @@ void CSoundHandler::release()
for (auto &chunk : soundChunks)
{
if (chunk.second)
Mix_FreeChunk(chunk.second);
if (chunk.second.first)
Mix_FreeChunk(chunk.second.first);
}
}
@ -142,14 +142,14 @@ Mix_Chunk *CSoundHandler::GetSoundChunk(std::string &sound, bool cache)
try
{
if (cache && soundChunks.find(sound) != soundChunks.end())
return soundChunks[sound];
return soundChunks[sound].first;
auto data = CResourceHandler::get()->load(ResourceID(std::string("SOUNDS/") + sound, EResType::SOUND))->readAll();
SDL_RWops *ops = SDL_RWFromMem(data.first.release(), data.second);
SDL_RWops *ops = SDL_RWFromMem(data.first.get(), data.second);
Mix_Chunk *chunk = Mix_LoadWAV_RW(ops, 1); // will free ops
if (cache)
soundChunks.insert(std::pair<std::string, Mix_Chunk *>(sound, chunk));
soundChunks.insert(std::pair<std::string, CachedChunk>(sound, std::make_pair (chunk, std::move (data.first))));
return chunk;
}

View File

@ -41,7 +41,8 @@ private:
SettingsListener listener;
void onVolumeChange(const JsonNode &volumeNode);
std::map<std::string, Mix_Chunk *> soundChunks;
using CachedChunk = std::pair<Mix_Chunk *, std::unique_ptr<ui8[]>>;
std::map<std::string, CachedChunk> soundChunks;
Mix_Chunk *GetSoundChunk(std::string &sound, bool cache);