From 238ca8cf0bf02453cb47a93e5310934ba395f23b Mon Sep 17 00:00:00 2001 From: outfoxxed Date: Fri, 31 May 2024 04:03:00 -0700 Subject: [PATCH] core/reloader: fix crashing on failed reload --- src/core/generation.cpp | 17 ++++++++++++----- src/core/rootwrapper.cpp | 4 ++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/core/generation.cpp b/src/core/generation.cpp index 71530430..e43db6ee 100644 --- a/src/core/generation.cpp +++ b/src/core/generation.cpp @@ -52,12 +52,14 @@ EngineGeneration::~EngineGeneration() { } void EngineGeneration::destroy() { - // Multiple generations can detect a reload at the same time. - QObject::disconnect(this->watcher, nullptr, this, nullptr); - this->watcher->deleteLater(); - this->watcher = nullptr; + if (this->watcher != nullptr) { + // Multiple generations can detect a reload at the same time. + QObject::disconnect(this->watcher, nullptr, this, nullptr); + this->watcher->deleteLater(); + this->watcher = nullptr; + } - if (this->engine != nullptr && this->root != nullptr) { + if (this->root != nullptr) { QObject::connect(this->root, &QObject::destroyed, this, [this]() { // prevent further js execution between garbage collection and engine destruction. this->engine->setInterrupted(true); @@ -74,6 +76,11 @@ void EngineGeneration::destroy() { this->root->deleteLater(); this->root = nullptr; + } else { + // the engine has never been used, no need to clean up + delete this->engine; + this->engine = nullptr; + delete this; } } diff --git a/src/core/rootwrapper.cpp b/src/core/rootwrapper.cpp index 3c69615f..1afb30cf 100644 --- a/src/core/rootwrapper.cpp +++ b/src/core/rootwrapper.cpp @@ -67,7 +67,7 @@ void RootWrapper::reloadGraph(bool hard) { if (obj == nullptr) { const QString error = "failed to create root component\n" + component.errorString(); qWarning().noquote() << error; - delete generation; + generation->destroy(); if (this->generation != nullptr && this->generation->qsgInstance != nullptr) { emit this->generation->qsgInstance->reloadFailed(error); @@ -81,7 +81,7 @@ void RootWrapper::reloadGraph(bool hard) { const QString error = "root component was not a Quickshell.ShellRoot"; qWarning().noquote() << error; delete obj; - delete generation; + generation->destroy(); if (this->generation != nullptr && this->generation->qsgInstance != nullptr) { emit this->generation->qsgInstance->reloadFailed(error);