1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-26 08:41:13 +02:00

Merge pull request #207 from krnowak/krnowak/leak-fix

Fix memory leak
This commit is contained in:
Alexander Shishkin 2016-08-31 15:58:26 +04:00 committed by GitHub
commit ce669c8a0a
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);