Skip to content

Commit

Permalink
Merge pull request #13 from Kenix3/main
Browse files Browse the repository at this point in the history
Fixes memory management issues in OTRLib
  • Loading branch information
Kenix3 committed Jan 4, 2022
2 parents c9d86e1 + cb9e959 commit 5790374
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 32 deletions.
4 changes: 2 additions & 2 deletions otrlib/OTRConfigFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ namespace OtrLib {
}

OTRConfigFile::~OTRConfigFile() {
(*this)["WINDOW"]["FULLSCREEN"] = std::to_string(OTRContext::GetInstance()->GetWindow()->IsFullscreen());

if (!Save()) {
spdlog::error("Failed to save configs!!!");
}

spdlog::info("destruct configfile");
}

mINI::INIMap<std::string>& OTRConfigFile::operator[](std::string Section) {
Expand Down
3 changes: 2 additions & 1 deletion otrlib/OTRConfigFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ namespace OtrLib {
bool remove(std::string Section);
void clear();
std::size_t size() const;
std::shared_ptr<OTRContext> GetContext() { return Context.lock(); }

protected:
bool CreateDefaultConfig();

private:
mINI::INIFile File;
mINI::INIStructure Val;
std::shared_ptr<OTRContext> Context;
std::weak_ptr<OTRContext> Context;
std::string Path;
};
}
2 changes: 2 additions & 0 deletions otrlib/OTRResourceMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace OtrLib {
fileCache.clear();
otrCache.clear();
gameResourceAddresses.clear();

spdlog::info("destruct resourcemgr");
}

std::shared_ptr<OTRFile> OTRResourceMgr::LoadFileFromCache(std::string filePath)
Expand Down
4 changes: 2 additions & 2 deletions otrlib/OTRResourceMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace OtrLib
OTRResourceMgr(std::shared_ptr<OTRContext> Context, std::string MainPath, std::string PatchesPath);
~OTRResourceMgr();


std::shared_ptr<OTRContext> GetContext() { return Context.lock(); }
char* LoadFileOriginal(std::string filePath);
DWORD LoadFile(uintptr_t destination, DWORD destinationSize, std::string filePath);
void MarkFileAsFree(uintptr_t destination, DWORD destinationSize, std::string filePath);
Expand All @@ -31,7 +31,7 @@ namespace OtrLib
std::shared_ptr<OTRFile> LoadFileFromCache(std::string filePath);

private:
std::shared_ptr<OTRContext> Context;
std::weak_ptr<OTRContext> Context;
std::map<std::string, std::shared_ptr<OTRFile>> fileCache;
std::map<std::string, std::shared_ptr<OTRResource>> otrCache;
std::map<std::string, std::shared_ptr<std::unordered_set<uintptr_t>>> gameResourceAddresses;
Expand Down
7 changes: 6 additions & 1 deletion otrlib/OTRWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ namespace OtrLib {

}

OTRWindow::~OTRWindow() {
spdlog::info("destruct window");
}

void OTRWindow::Init() {
std::shared_ptr<OTRConfigFile> pConf = OTRContext::GetInstance()->GetConfig();
OTRConfigFile& Conf = *pConf.get();
Expand All @@ -81,7 +85,7 @@ namespace OtrLib {
dwWidth = OtrLib::stoi(Conf["WINDOW"]["FULLSCREEN WIDTH"], 1920);
dwHeight = OtrLib::stoi(Conf["WINDOW"]["FULLSCREEN HEIGHT"], 1080);

gfx_init(WmApi, RenderingApi, Context->GetName().c_str(), bIsFullscreen);
gfx_init(WmApi, RenderingApi, GetContext()->GetName().c_str(), bIsFullscreen);
WmApi->set_fullscreen_changed_callback(OTRWindow::OnFullscreenChanged);
WmApi->set_keyboard_callbacks(OTRWindow::KeyDown, OTRWindow::KeyUp, OTRWindow::AllKeysUp);
}
Expand Down Expand Up @@ -158,6 +162,7 @@ namespace OtrLib {
OTRConfigFile& Conf = *pConf.get();

OTRContext::GetInstance()->GetWindow()->bIsFullscreen = bIsFullscreen;
Conf["WINDOW"]["FULLSCREEN"] = std::to_string(OTRContext::GetInstance()->GetWindow()->IsFullscreen());
}

int32_t OTRWindow::GetCurrentWidth() {
Expand Down
3 changes: 2 additions & 1 deletion otrlib/OTRWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace OtrLib {
static std::shared_ptr<OtrLib::OTRController> Controllers[MAXCONTROLLERS];

OTRWindow(std::shared_ptr<OTRContext> Context);
~OTRWindow();
void MainLoop(void (*MainFunction)(void));
void Init();
void RunCommands(Gfx* Commands);
Expand All @@ -33,7 +34,7 @@ namespace OtrLib {
static void AllKeysUp(void);
static void OnFullscreenChanged(bool bIsNowFullscreen);

std::shared_ptr<OTRContext> Context;
std::weak_ptr<OTRContext> Context;

GfxWindowManagerAPI* WmApi;
GfxRenderingAPI* RenderingApi;
Expand Down
50 changes: 29 additions & 21 deletions otrlib/OtrContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,58 @@
#include "spdlog/sinks/basic_file_sink.h"

namespace OtrLib {
std::shared_ptr<OTRContext> OTRContext::Context = nullptr;
std::weak_ptr<OTRContext> OTRContext::Context;

std::shared_ptr<OTRContext> OTRContext::GetInstance() {
return Context;
return Context.lock();
}

std::shared_ptr<OTRContext> OTRContext::CreateInstance(std::string Name, std::string MainPath, std::string PatchesPath) {
if (Context == nullptr) {
if (Context.expired()) {
if (!MainPath.empty()) {
Context = std::make_shared<OTRContext>(Name, MainPath, PatchesPath);
auto Shared = std::make_shared<OTRContext>(Name, MainPath, PatchesPath);
Context = Shared;
Shared->InitWindow();

return Shared;
} else {
spdlog::error("No Main Archive passed to create instance");
}
} else {
spdlog::debug("Trying to create a context when it already exists.");
}

return Context;
return GetInstance();
}

OTRContext::OTRContext(std::string Name, std::string MainPath, std::string PatchesPath) : Name(Name), MainPath(MainPath), PatchesPath(PatchesPath) {
InitLogging();
}

OTRContext::~OTRContext() {
spdlog::info("destruct OTRContext");

Window = nullptr;
ResourceMgr = nullptr;
Config = nullptr;
}

void OTRContext::InitWindow() {
ResourceMgr = std::make_shared<OTRResourceMgr>(OTRContext::GetInstance(), MainPath, PatchesPath);
Window = std::make_shared<OTRWindow>(OTRContext::GetInstance());
Config = std::make_shared<OTRConfigFile>(OTRContext::GetInstance(), "otr.ini");
}

OTRContext::OTRContext(std::string Name, std::string MainPath, std::string PatchesPath) : Name(Name) {
void OTRContext::InitLogging() {
try {
// Setup Logging
Logger = spdlog::create_async<spdlog::sinks::basic_file_sink_mt>("async_file_logger", "logs/" + Name + ".log");
Logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%@] [%l] %v");
spdlog::set_level(spdlog::level::info);
spdlog::set_level(spdlog::level::trace);
spdlog::set_default_logger(Logger);
}
catch (const spdlog::spdlog_ex& ex) {
std::cout << "Log initialization failed: " << ex.what() << std::endl;
}

ResourceMgr = std::make_shared<OTRResourceMgr>(std::make_shared<OTRContext>(*this), MainPath, PatchesPath);
Window = std::make_shared<OTRWindow>(std::make_shared<OTRContext>(*this));
Config = std::make_shared<OTRConfigFile>(std::make_shared<OTRContext>(*this), "otr.ini");
}

OTRContext::~OTRContext() {
// Kill Logging
try {
spdlog::drop(Name);
}
catch (const spdlog::spdlog_ex& ex) {
std::cout << "Log de-initialization failed: " << ex.what() << std::endl;
}
}
}
7 changes: 5 additions & 2 deletions otrlib/OtrContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ namespace OtrLib {
~OTRContext();

protected:
void InitWindow();
void InitLogging();

private:
static std::shared_ptr<OTRContext> Context;

static std::weak_ptr <OTRContext> Context;
std::shared_ptr<spdlog::logger> Logger;
std::shared_ptr<OTRWindow> Window;
std::shared_ptr<OTRConfigFile> Config; // Config needs to be after the Window because we call the Window during it's destructor.
std::shared_ptr<OTRResourceMgr> ResourceMgr;
std::string Name;
std::string MainPath;
std::string PatchesPath;
};
}
4 changes: 2 additions & 2 deletions otrlib/otrlib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;ENABLE_OPENGL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<LanguageStandard_C>stdc11</LanguageStandard_C>
<LanguageStandard_C>stdc17</LanguageStandard_C>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile>
Expand Down Expand Up @@ -126,7 +126,7 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;ENABLE_OPENGL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpplatest</LanguageStandard>
<LanguageStandard>stdcpp20</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
</ClCompile>
Expand Down

0 comments on commit 5790374

Please sign in to comment.