diff --git a/lib/libimhex/include/hex/api/imhex_api.hpp b/lib/libimhex/include/hex/api/imhex_api.hpp index 204ab1ad4..bbc8d5c0a 100644 --- a/lib/libimhex/include/hex/api/imhex_api.hpp +++ b/lib/libimhex/include/hex/api/imhex_api.hpp @@ -19,6 +19,8 @@ struct GLFWwindow; namespace hex { + class AutoResetBase; + namespace prv { class Provider; } @@ -407,6 +409,9 @@ namespace hex { bool isWindowResizable(); + void addAutoResetObject(AutoResetBase *object); + void cleanup(); + } /** diff --git a/lib/libimhex/include/hex/helpers/auto_reset.hpp b/lib/libimhex/include/hex/helpers/auto_reset.hpp index 2981ccc8b..9e04aebe7 100644 --- a/lib/libimhex/include/hex/helpers/auto_reset.hpp +++ b/lib/libimhex/include/hex/helpers/auto_reset.hpp @@ -1,22 +1,23 @@ #pragma once #include +#include namespace hex { + class AutoResetBase { + public: + virtual ~AutoResetBase() = default; + virtual void reset() = 0; + }; + template - class AutoReset { + class AutoReset : AutoResetBase { public: using Type = T; AutoReset() { - EventImHexClosing::subscribe(this, [this] { - this->reset(); - }); - } - - ~AutoReset() { - EventImHexClosing::unsubscribe(this); + ImHexApi::System::impl::addAutoResetObject(this); } T* operator->() { @@ -53,8 +54,16 @@ namespace hex { return m_value; } - void reset() { - m_value = T(); + void reset() override { + if constexpr (requires { m_value.reset(); }) { + m_value.reset(); + } else if constexpr (requires { m_value.clear(); }) { + m_value.clear(); + } else if constexpr (requires(T t) { t = nullptr; }) { + m_value = nullptr; + } else { + m_value = { }; + } } private: diff --git a/lib/libimhex/source/api/content_registry.cpp b/lib/libimhex/source/api/content_registry.cpp index d32295cf4..5620179a6 100644 --- a/lib/libimhex/source/api/content_registry.cpp +++ b/lib/libimhex/source/api/content_registry.cpp @@ -106,13 +106,15 @@ namespace hex { if (!loaded) store(); - for (const auto &[category, rest] : *impl::s_onChangeCallbacks) { - for (const auto &[name, callbacks] : rest) { - for (const auto &[id, callback] : callbacks) { - callback(getSetting(category, name, {})); + TaskManager::doLater([] { + for (const auto &[category, rest] : *impl::s_onChangeCallbacks) { + for (const auto &[name, callbacks] : rest) { + for (const auto &[id, callback] : callbacks) { + callback(getSetting(category, name, {})); + } } } - } + }); } void store() { diff --git a/lib/libimhex/source/api/imhex_api.cpp b/lib/libimhex/source/api/imhex_api.cpp index 5d1791c25..e5e0221b7 100644 --- a/lib/libimhex/source/api/imhex_api.cpp +++ b/lib/libimhex/source/api/imhex_api.cpp @@ -471,6 +471,16 @@ namespace hex { return s_windowResizable; } + static std::vector s_autoResetObjects; + void addAutoResetObject(AutoResetBase *object) { + s_autoResetObjects.emplace_back(object); + } + + void cleanup() { + for (const auto &object : s_autoResetObjects) + object->reset(); + } + } diff --git a/main/gui/source/init/tasks.cpp b/main/gui/source/init/tasks.cpp index 973f1c238..c299a66dd 100644 --- a/main/gui/source/init/tasks.cpp +++ b/main/gui/source/init/tasks.cpp @@ -80,6 +80,7 @@ namespace hex::init { log::fatal("To the person fixing this, read the comment above this message for more information."); }); + ImHexApi::System::impl::cleanup(); EventImHexClosing::post(); EventManager::clear();