Browse Source

Fix mem leak in storm.cpp

pull/2627/head
Vladimir Olteanu 5 years ago committed by Anders Jenbo
parent
commit
f1490b7826
  1. 2
      Source/init.cpp
  2. 12
      Source/storm/storm.cpp
  3. 3
      Source/storm/storm.h

2
Source/init.cpp

@ -61,7 +61,7 @@ HANDLE LoadMPQ(const std::vector<std::string> &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) {

12
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<std::string> 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<unsigned char>(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)

3
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);

Loading…
Cancel
Save