1
0
mirror of synced 2024-11-28 09:30:51 +01:00

fix: Crash when restarting ImHex more than once

This commit is contained in:
WerWolv 2024-02-19 22:06:46 +01:00
parent 218946d5de
commit adc51d3773
5 changed files with 42 additions and 15 deletions

View File

@ -19,6 +19,8 @@ struct GLFWwindow;
namespace hex { namespace hex {
class AutoResetBase;
namespace prv { namespace prv {
class Provider; class Provider;
} }
@ -407,6 +409,9 @@ namespace hex {
bool isWindowResizable(); bool isWindowResizable();
void addAutoResetObject(AutoResetBase *object);
void cleanup();
} }
/** /**

View File

@ -1,22 +1,23 @@
#pragma once #pragma once
#include <hex/api/event_manager.hpp> #include <hex/api/event_manager.hpp>
#include <hex/api/imhex_api.hpp>
namespace hex { namespace hex {
class AutoResetBase {
public:
virtual ~AutoResetBase() = default;
virtual void reset() = 0;
};
template<typename T> template<typename T>
class AutoReset { class AutoReset : AutoResetBase {
public: public:
using Type = T; using Type = T;
AutoReset() { AutoReset() {
EventImHexClosing::subscribe(this, [this] { ImHexApi::System::impl::addAutoResetObject(this);
this->reset();
});
}
~AutoReset() {
EventImHexClosing::unsubscribe(this);
} }
T* operator->() { T* operator->() {
@ -53,8 +54,16 @@ namespace hex {
return m_value; return m_value;
} }
void reset() { void reset() override {
m_value = T(); 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: private:

View File

@ -106,13 +106,15 @@ namespace hex {
if (!loaded) if (!loaded)
store(); store();
for (const auto &[category, rest] : *impl::s_onChangeCallbacks) { TaskManager::doLater([] {
for (const auto &[name, callbacks] : rest) { for (const auto &[category, rest] : *impl::s_onChangeCallbacks) {
for (const auto &[id, callback] : callbacks) { for (const auto &[name, callbacks] : rest) {
callback(getSetting(category, name, {})); for (const auto &[id, callback] : callbacks) {
callback(getSetting(category, name, {}));
}
} }
} }
} });
} }
void store() { void store() {

View File

@ -471,6 +471,16 @@ namespace hex {
return s_windowResizable; return s_windowResizable;
} }
static std::vector<AutoResetBase*> s_autoResetObjects;
void addAutoResetObject(AutoResetBase *object) {
s_autoResetObjects.emplace_back(object);
}
void cleanup() {
for (const auto &object : s_autoResetObjects)
object->reset();
}
} }

View File

@ -80,6 +80,7 @@ namespace hex::init {
log::fatal("To the person fixing this, read the comment above this message for more information."); log::fatal("To the person fixing this, read the comment above this message for more information.");
}); });
ImHexApi::System::impl::cleanup();
EventImHexClosing::post(); EventImHexClosing::post();
EventManager::clear(); EventManager::clear();