From 55f56e4465ab45d54e95562fe40315a2dc544af6 Mon Sep 17 00:00:00 2001 From: Raul Radu <raul.radu@mail.polimi.it> Date: Tue, 9 Jan 2024 02:11:00 +0100 Subject: [PATCH] [Window] Changed how window handles modules Now every window will maintain a shared_ptr of a module in order to prevent it from being deleted and automatically delete it when the window is destroyed --- src/shared/Core/Window/Window.cpp | 28 +++++++++++++++------------- src/shared/Core/Window/Window.h | 10 +++++++--- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/shared/Core/Window/Window.cpp b/src/shared/Core/Window/Window.cpp index 9cce6468..87f0f4c7 100644 --- a/src/shared/Core/Window/Window.cpp +++ b/src/shared/Core/Window/Window.cpp @@ -23,9 +23,10 @@ Window::Window(XmlObject configuration) { - errorDisplayer = new ErrorDisplayer(this); - Module *module = ModulesList::getInstance().instantiateModule( - configuration.getObjectName()); + errorDisplayer = std::make_shared<ErrorDisplayer, QWidget *>(this); + std::shared_ptr<Module> module = + ModulesList::getInstance().instantiateModule( + configuration.getObjectName()); if (module) { @@ -34,22 +35,23 @@ Window::Window(XmlObject configuration) } } -void Window::setChild(Module *module) +void Window::setChild(std::shared_ptr<Module> module) { - setCentralWidget(module); + this->activeModule = module; + setCentralWidget(module.get()); module->lower(); - - connect(module, &Module::replaceMe, this, &Window::replaceChild); - connect(module, &Module::closeMe, this, &Window::closeChild); + connect(module.get(), &Module::replaceMe, this, &Window::replaceChild); + connect(module.get(), &Module::closeMe, this, &Window::closeChild); } -void Window::replaceChild(Module *oldModule, Module *newModule) +void Window::replaceChild(std::shared_ptr<Module> _oldModule, + std::shared_ptr<Module> newModule) { - // Set the new module + std::shared_ptr<Module> oldModule = this->activeModule; + setChild(newModule); - // Delete the old child - delete oldModule; + oldModule.reset(); } void Window::closeChild(Module *module) { close(); } @@ -63,4 +65,4 @@ void Window::closeEvent(QCloseEvent *event) { emit onWindowClosed(); event->accept(); -} \ No newline at end of file +} diff --git a/src/shared/Core/Window/Window.h b/src/shared/Core/Window/Window.h index 6a061e54..66b35021 100644 --- a/src/shared/Core/Window/Window.h +++ b/src/shared/Core/Window/Window.h @@ -24,6 +24,7 @@ #include <QCloseEvent> #include <QMainWindow> #include <QMenu> +#include <memory> #include "Components/ErrorDisplayer/ErrorDisplayer.h" @@ -36,7 +37,7 @@ public: XmlObject toXmlObject(); - ErrorDisplayer *errorDisplayer; + std::shared_ptr<ErrorDisplayer> errorDisplayer; void closeEvent(QCloseEvent *event) override; @@ -44,9 +45,12 @@ signals: void onWindowClosed(); private: - void setChild(Module *module); + std::shared_ptr<Module> activeModule; + + void setChild(std::shared_ptr<Module> module); private slots: - void replaceChild(Module *oldModule, Module *newModule); + void replaceChild(std::shared_ptr<Module> oldModule, + std::shared_ptr<Module> newModule); void closeChild(Module *module); }; -- GitLab