From f1490b782646c4e2a3dcea7379f77fec719b893d Mon Sep 17 00:00:00 2001 From: Vladimir Olteanu Date: Mon, 16 Aug 2021 12:38:59 +0300 Subject: [PATCH] Fix mem leak in storm.cpp --- Source/init.cpp | 2 +- Source/storm/storm.cpp | 12 +++++------- Source/storm/storm.h | 3 ++- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/Source/init.cpp b/Source/init.cpp index a7ea24371..fdbbd5921 100644 --- a/Source/init.cpp +++ b/Source/init.cpp @@ -61,7 +61,7 @@ HANDLE LoadMPQ(const std::vector &paths, const char *mpqName) mpqAbsPath = path + mpqName; if (SFileOpenArchive(mpqAbsPath.c_str(), 0, MPQ_OPEN_READ_ONLY, &archive)) { LogVerbose(" Found: {} in {}", mpqName, path); - SFileSetBasePath(path.c_str()); + SFileSetBasePath(path); return archive; } if (SErrGetLastError() != STORM_ERROR_FILE_NOT_FOUND) { diff --git a/Source/storm/storm.cpp b/Source/storm/storm.cpp index 00ad21f55..5914e8b35 100644 --- a/Source/storm/storm.cpp +++ b/Source/storm/storm.cpp @@ -13,6 +13,7 @@ #include "utils/paths.h" #include "utils/sdl_mutex.h" #include "utils/stubs.h" +#include "utils/stdcompat/optional.hpp" // Include Windows headers for Get/SetLastError. #if defined(_WIN32) @@ -30,7 +31,7 @@ namespace devilution { namespace { bool directFileAccess = false; -std::string *SBasePath = nullptr; +std::optional SBasePath; SdlMutex Mutex; @@ -81,7 +82,7 @@ bool SFileOpenFile(const char *filename, HANDLE *phFile) { bool result = false; - if (directFileAccess && SBasePath != nullptr) { + if (directFileAccess && SBasePath) { std::string path = *SBasePath + filename; for (std::size_t i = SBasePath->size(); i < path.size(); ++i) path[i] = AsciiToLowerTable_Path[static_cast(path[i])]; @@ -148,12 +149,9 @@ void SErrSetLastError(uint32_t dwErrCode) ::SetLastError(dwErrCode); } -bool SFileSetBasePath(const char *path) +void SFileSetBasePath(string_view path) { - if (SBasePath == nullptr) - SBasePath = new std::string; - *SBasePath = path; - return true; + SBasePath.emplace(path); } bool SFileEnableDirectAccess(bool enable) diff --git a/Source/storm/storm.h b/Source/storm/storm.h index 29553ef48..ed907f4ad 100644 --- a/Source/storm/storm.h +++ b/Source/storm/storm.h @@ -8,6 +8,7 @@ #include "appfat.h" #include "multi.h" +#include "utils/stdcompat/string_view.hpp" namespace devilution { @@ -245,7 +246,7 @@ void SErrSetLastError(uint32_t dwErrCode); */ void SStrCopy(char *dest, const char *src, int max_length); -bool SFileSetBasePath(const char *); +void SFileSetBasePath(string_view path); bool SNetGetOwnerTurnsWaiting(uint32_t *); bool SNetUnregisterEventHandler(event_type); bool SNetRegisterEventHandler(event_type, SEVTHANDLER);