From 6efa05a8eb3f8e5239b9c379b29a135005ca8cd8 Mon Sep 17 00:00:00 2001 From: outfoxxed Date: Tue, 18 Jun 2024 20:58:33 -0700 Subject: [PATCH] core: run full destruction sequence before exiting Fixes QTimer messages. --- src/core/generation.cpp | 31 ++++++++++++++++++++++++++++--- src/core/generation.hpp | 6 ++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/core/generation.cpp b/src/core/generation.cpp index aef1204..0a62432 100644 --- a/src/core/generation.cpp +++ b/src/core/generation.cpp @@ -52,6 +52,9 @@ EngineGeneration::~EngineGeneration() { } void EngineGeneration::destroy() { + if (this->destroying) return; + this->destroying = true; + if (this->watcher != nullptr) { // Multiple generations can detect a reload at the same time. QObject::disconnect(this->watcher, nullptr, this, nullptr); @@ -71,7 +74,12 @@ void EngineGeneration::destroy() { delete this->engine; this->engine = nullptr; + + auto terminate = this->shouldTerminate; + auto code = this->exitCode; delete this; + + if (terminate) QCoreApplication::exit(code); }); this->root->deleteLater(); @@ -80,11 +88,18 @@ void EngineGeneration::destroy() { // the engine has never been used, no need to clean up delete this->engine; this->engine = nullptr; + + auto terminate = this->shouldTerminate; + auto code = this->exitCode; delete this; + + if (terminate) QCoreApplication::exit(code); } } void EngineGeneration::shutdown() { + if (this->destroying) return; + delete this->root; this->root = nullptr; delete this->engine; @@ -100,9 +115,8 @@ void EngineGeneration::onReload(EngineGeneration* old) { old->assignIncubationController(); } - auto* app = QCoreApplication::instance(); - QObject::connect(this->engine, &QQmlEngine::quit, app, &QCoreApplication::quit); - QObject::connect(this->engine, &QQmlEngine::exit, app, &QCoreApplication::exit); + QObject::connect(this->engine, &QQmlEngine::quit, this, &EngineGeneration::quit); + QObject::connect(this->engine, &QQmlEngine::exit, this, &EngineGeneration::exit); this->root->reload(old == nullptr ? nullptr : old->root); this->singletonRegistry.onReload(old == nullptr ? nullptr : &old->singletonRegistry); @@ -266,6 +280,17 @@ void EngineGeneration::incubationControllerDestroyed() { } } +void EngineGeneration::quit() { + this->shouldTerminate = true; + this->destroy(); +} + +void EngineGeneration::exit(int code) { + this->shouldTerminate = true; + this->exitCode = code; + this->destroy(); +} + void EngineGeneration::assignIncubationController() { QQmlIncubationController* controller = nullptr; if (this->incubationControllers.isEmpty()) controller = &this->delayedIncubationController; diff --git a/src/core/generation.hpp b/src/core/generation.hpp index 760c19c..2f2fc5f 100644 --- a/src/core/generation.hpp +++ b/src/core/generation.hpp @@ -61,9 +61,15 @@ private slots: void onFileChanged(const QString& name); void onDirectoryChanged(); void incubationControllerDestroyed(); + void quit(); + void exit(int code); private: void postReload(); void assignIncubationController(); QVector> incubationControllers; + + bool destroying = false; + bool shouldTerminate = false; + int exitCode = 0; };