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 {
class AutoResetBase;
namespace prv {
class Provider;
}
@ -407,6 +409,9 @@ namespace hex {
bool isWindowResizable();
void addAutoResetObject(AutoResetBase *object);
void cleanup();
}
/**

View File

@ -1,22 +1,23 @@
#pragma once
#include <hex/api/event_manager.hpp>
#include <hex/api/imhex_api.hpp>
namespace hex {
class AutoResetBase {
public:
virtual ~AutoResetBase() = default;
virtual void reset() = 0;
};
template<typename T>
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:

View File

@ -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() {

View File

@ -471,6 +471,16 @@ namespace hex {
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.");
});
ImHexApi::System::impl::cleanup();
EventImHexClosing::post();
EventManager::clear();