From c28522b7efcf8cf0ac68070ed99677d7b50521b4 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 16 May 2024 19:00:31 +0200 Subject: [PATCH] fix: Settings sometimes being reset on shutdown --- lib/libimhex/include/hex/helpers/auto_reset.hpp | 10 ++++++++++ lib/libimhex/source/api/content_registry.cpp | 5 ++++- lib/libimhex/source/api/imhex_api.cpp | 2 +- lib/libimhex/source/api/layout_manager.cpp | 4 ++-- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/libimhex/include/hex/helpers/auto_reset.hpp b/lib/libimhex/include/hex/helpers/auto_reset.hpp index fff597139..98a930471 100644 --- a/lib/libimhex/include/hex/helpers/auto_reset.hpp +++ b/lib/libimhex/include/hex/helpers/auto_reset.hpp @@ -58,6 +58,13 @@ namespace hex { return m_value; } + bool isValid() const { + return m_valid; + } + + private: + friend void ImHexApi::System::impl::cleanup(); + void reset() override { if constexpr (requires { m_value.reset(); }) { m_value.reset(); @@ -68,9 +75,12 @@ namespace hex { } else { m_value = { }; } + + m_valid = false; } private: + bool m_valid = true; T m_value; }; diff --git a/lib/libimhex/source/api/content_registry.cpp b/lib/libimhex/source/api/content_registry.cpp index 5c7628e87..b866831c4 100644 --- a/lib/libimhex/source/api/content_registry.cpp +++ b/lib/libimhex/source/api/content_registry.cpp @@ -128,7 +128,10 @@ namespace hex { } void store() { - const auto &settingsData = getSettingsData(); + if (!s_settings.isValid()) + return; + + const auto &settingsData = *s_settings; // During a crash settings can be empty, causing them to be overwritten. if (settingsData.empty()) { diff --git a/lib/libimhex/source/api/imhex_api.cpp b/lib/libimhex/source/api/imhex_api.cpp index 1120e9308..d77fdc1af 100644 --- a/lib/libimhex/source/api/imhex_api.cpp +++ b/lib/libimhex/source/api/imhex_api.cpp @@ -216,7 +216,7 @@ namespace hex { } void clearSelection() { - impl::s_currentSelection.reset(); + impl::s_currentSelection->reset(); } void setSelection(const Region ®ion, prv::Provider *provider) { diff --git a/lib/libimhex/source/api/layout_manager.cpp b/lib/libimhex/source/api/layout_manager.cpp index 4a9f0ce42..0844cc143 100644 --- a/lib/libimhex/source/api/layout_manager.cpp +++ b/lib/libimhex/source/api/layout_manager.cpp @@ -134,8 +134,8 @@ namespace hex { } void LayoutManager::reset() { - s_layoutPathToLoad.reset(); - s_layoutStringToLoad.reset(); + s_layoutPathToLoad->reset(); + s_layoutStringToLoad->reset(); s_layouts->clear(); }