core/reloader: fix crashing on failed reload

This commit is contained in:
outfoxxed 2024-05-31 04:03:00 -07:00
parent a8506edbb9
commit 238ca8cf0b
Signed by: outfoxxed
GPG key ID: 4C88A185FB89301E
2 changed files with 14 additions and 7 deletions

View file

@ -52,12 +52,14 @@ EngineGeneration::~EngineGeneration() {
} }
void EngineGeneration::destroy() { void EngineGeneration::destroy() {
// Multiple generations can detect a reload at the same time. if (this->watcher != nullptr) {
QObject::disconnect(this->watcher, nullptr, this, nullptr); // Multiple generations can detect a reload at the same time.
this->watcher->deleteLater(); QObject::disconnect(this->watcher, nullptr, this, nullptr);
this->watcher = 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]() { QObject::connect(this->root, &QObject::destroyed, this, [this]() {
// prevent further js execution between garbage collection and engine destruction. // prevent further js execution between garbage collection and engine destruction.
this->engine->setInterrupted(true); this->engine->setInterrupted(true);
@ -74,6 +76,11 @@ void EngineGeneration::destroy() {
this->root->deleteLater(); this->root->deleteLater();
this->root = nullptr; this->root = nullptr;
} else {
// the engine has never been used, no need to clean up
delete this->engine;
this->engine = nullptr;
delete this;
} }
} }

View file

@ -67,7 +67,7 @@ void RootWrapper::reloadGraph(bool hard) {
if (obj == nullptr) { if (obj == nullptr) {
const QString error = "failed to create root component\n" + component.errorString(); const QString error = "failed to create root component\n" + component.errorString();
qWarning().noquote() << error; qWarning().noquote() << error;
delete generation; generation->destroy();
if (this->generation != nullptr && this->generation->qsgInstance != nullptr) { if (this->generation != nullptr && this->generation->qsgInstance != nullptr) {
emit this->generation->qsgInstance->reloadFailed(error); 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"; const QString error = "root component was not a Quickshell.ShellRoot";
qWarning().noquote() << error; qWarning().noquote() << error;
delete obj; delete obj;
delete generation; generation->destroy();
if (this->generation != nullptr && this->generation->qsgInstance != nullptr) { if (this->generation != nullptr && this->generation->qsgInstance != nullptr) {
emit this->generation->qsgInstance->reloadFailed(error); emit this->generation->qsgInstance->reloadFailed(error);