From 9ca31329f919f2c040a3e972da9762de0dbf4ea7 Mon Sep 17 00:00:00 2001 From: BenCat07 Date: Wed, 27 May 2020 10:55:16 +0200 Subject: [PATCH] Fix crashes after detaching with uncleared event callbacks --- include/visual/menu/GuiInterface.hpp | 3 ++- src/hack.cpp | 5 +++++ src/visual/EventLogging.cpp | 7 ++++++- src/visual/menu/GuiInterface.cpp | 5 +++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/include/visual/menu/GuiInterface.hpp b/include/visual/menu/GuiInterface.hpp index ed64781c..01dfc60b 100644 --- a/include/visual/menu/GuiInterface.hpp +++ b/include/visual/menu/GuiInterface.hpp @@ -10,8 +10,9 @@ namespace gui { void init(); +void shutdown(); void draw(); bool handleSdlEvent(SDL_Event *event); void onLevelLoad(); -} // namespace gui \ No newline at end of file +} // namespace gui diff --git a/src/hack.cpp b/src/hack.cpp index 65132a24..34b565a3 100644 --- a/src/hack.cpp +++ b/src/hack.cpp @@ -17,6 +17,7 @@ #include "hack.hpp" #include "common.hpp" #include "MiscTemporary.hpp" +#include "menu/GuiInterface.hpp" #include #include @@ -400,6 +401,10 @@ void hack::Shutdown() delete g_pLocalPlayer; delete g_pTeamRoundTimer; delete g_pPlayerResource; +#if ENABLE_GUI + logging::Info("Shutting down GUI"); + gui::shutdown(); +#endif if (!hack::game_shutdown) { logging::Info("Running shutdown handlers"); diff --git a/src/visual/EventLogging.cpp b/src/visual/EventLogging.cpp index 9828b653..15aaf896 100644 --- a/src/visual/EventLogging.cpp +++ b/src/visual/EventLogging.cpp @@ -9,6 +9,7 @@ #include #include #include "KeyValues.h" +#include "HookTools.hpp" namespace event_logging { @@ -253,7 +254,11 @@ public: static LoggingEventListener event_listener{}; -InitRoutine init([]() { g_IGameEventManager->AddListener(&event_listener, false); }); +InitRoutine init([]() { + g_IGameEventManager->AddListener(&event_listener, false); + EC::Register( + EC::Shutdown, []() { g_IGameEventManager->RemoveListener(&event_listener); }, "shutdown_eventlogger"); +}); bool isEnabled() { diff --git a/src/visual/menu/GuiInterface.cpp b/src/visual/menu/GuiInterface.cpp index 01679fee..32148523 100644 --- a/src/visual/menu/GuiInterface.cpp +++ b/src/visual/menu/GuiInterface.cpp @@ -146,6 +146,11 @@ void gui::init() init_done = true; } +void gui::shutdown() +{ + g_IGameEventManager->RemoveListener(&listener); +} + void gui::draw() { if (!init_done)