From 27eb38df8d6ee5e3660d60825459bc389598b824 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Mon, 24 Nov 2014 14:08:21 -0800 Subject: [PATCH] Fix a possible leak. Can definitely see this happening. --- Core/Loaders.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Core/Loaders.cpp b/Core/Loaders.cpp index b546b9d154..08e42a7b4d 100644 --- a/Core/Loaders.cpp +++ b/Core/Loaders.cpp @@ -491,7 +491,13 @@ void CachingFileLoader::SaveIntoCache(s64 pos, size_t bytes, bool readingAhead) backendMutex_.unlock(); blocksMutex_.lock(); - blocks_[cacheStartPos] = BlockInfo(buf); + // While blocksMutex_ was unlocked, another thread may have read. + // If so, free the one we just read. + if (blocks_.find(cacheStartPos) == blocks_.end()) { + blocks_[cacheStartPos] = BlockInfo(buf); + } else { + delete [] buf; + } } else { blocksMutex_.unlock(); @@ -502,6 +508,10 @@ void CachingFileLoader::SaveIntoCache(s64 pos, size_t bytes, bool readingAhead) blocksMutex_.lock(); for (size_t i = 0; i < blocksToRead; ++i) { + if (blocks_.find(cacheStartPos + i) != blocks_.end()) { + // Written while we were busy, just skip it. Keep the existing block. + continue; + } u8 *buf = new u8[BLOCK_SIZE]; memcpy(buf, wholeRead + (i << BLOCK_SHIFT), BLOCK_SIZE); blocks_[cacheStartPos + i] = BlockInfo(buf);