From 1e547da66fd3a428cd60bb271dca56824b40563f Mon Sep 17 00:00:00 2001 From: Dmitry Pinaev Date: Sun, 22 Nov 2026 21:38:19 +0100 Subject: [PATCH] Delete embedded widgets in the model destructor, through a QPointer The registry builds a temporary model to read its name. A widget made in the constructor of that model was never embedded, so it leaked. Deleting the widget in the destructor frees it. After embedding, the node proxy owns the widget and may delete it first. A raw pointer would then be deleted twice. A QPointer is null after the widget is destroyed, so the destructor can always delete it. Update all examples that hold a widget, add the ownership rules to the data-flow guide and add a regression test. Fixes paceholder/nodeeditor#248 --- docs/guide/data-flow.rst | 24 ++- .../calculator/NumberDisplayDataModel.cpp | 5 + .../calculator/NumberDisplayDataModel.hpp | 5 +- examples/calculator/NumberSourceDataModel.cpp | 5 + examples/calculator/NumberSourceDataModel.hpp | 5 +- examples/node_validation/TextDisplayModel.hpp | 5 +- examples/node_validation/TextSourceModel.hpp | 5 +- examples/node_validation/ValidatedModel.hpp | 8 +- .../resizable_images/ImageLoaderModel.hpp | 5 +- examples/resizable_images/ImageShowModel.hpp | 5 +- examples/text/TextDisplayDataModel.hpp | 5 +- examples/text/TextSourceDataModel.cpp | 5 + examples/text/TextSourceDataModel.hpp | 5 +- test/CMakeLists.txt | 1 + test/src/TestEmbeddedWidgetOwnership.cpp | 152 ++++++++++++++++++ 15 files changed, 224 insertions(+), 16 deletions(-) create mode 100644 test/src/TestEmbeddedWidgetOwnership.cpp diff --git a/docs/guide/data-flow.rst b/docs/guide/data-flow.rst index daf6603e7..b8a2c19af 100644 --- a/docs/guide/data-flow.rst +++ b/docs/guide/data-flow.rst @@ -217,6 +217,8 @@ Add interactive controls to your nodes: class NumberSourceNode : public NodeDelegateModel { public: + ~NumberSourceNode() override { delete _spinBox; } + QWidget* embeddedWidget() override { if (!_spinBox) { @@ -235,7 +237,7 @@ Add interactive controls to your nodes: } private: - QDoubleSpinBox* _spinBox = nullptr; + QPointer _spinBox; std::shared_ptr _data; }; @@ -243,6 +245,26 @@ Add interactive controls to your nodes: :alt: Node with embedded spin box :width: 400px +Widget ownership +~~~~~~~~~~~~~~~~ + +Two objects can own the widget, and either can be destroyed first: + +- The model creates the widget. When the node is shown, its ``QGraphicsProxyWidget`` takes + it over and deletes it with the node. +- A widget that is never shown has no other owner. The registry builds a temporary model to + read its ``name()``. If the constructor creates the widget, only the model can free it. + +Follow these rules: + +- Create the widget lazily in ``embeddedWidget()``, as above. The temporary model never calls + it, so it never makes a widget. +- Delete the widget in the model destructor. This frees a widget that was never shown. +- Keep the widget in a ``QPointer``. A ``QPointer`` does not delete anything. It follows the + ``QObject`` and becomes null when the object is destroyed, whoever destroys it. If the proxy + has already deleted the widget, the destructor ``delete`` does nothing. With a raw pointer + it would delete the widget twice and crash. + Validation State ---------------- diff --git a/examples/calculator/NumberDisplayDataModel.cpp b/examples/calculator/NumberDisplayDataModel.cpp index 5086050c8..aa1b1c95c 100644 --- a/examples/calculator/NumberDisplayDataModel.cpp +++ b/examples/calculator/NumberDisplayDataModel.cpp @@ -6,6 +6,11 @@ NumberDisplayDataModel::NumberDisplayDataModel() : _label{nullptr} {} +NumberDisplayDataModel::~NumberDisplayDataModel() +{ + delete _label; +} + unsigned int NumberDisplayDataModel::nPorts(PortType portType) const { unsigned int result = 1; diff --git a/examples/calculator/NumberDisplayDataModel.hpp b/examples/calculator/NumberDisplayDataModel.hpp index 78f984f5f..65afd2bdf 100644 --- a/examples/calculator/NumberDisplayDataModel.hpp +++ b/examples/calculator/NumberDisplayDataModel.hpp @@ -3,6 +3,7 @@ #include #include +#include #include @@ -25,7 +26,7 @@ class NumberDisplayDataModel : public NodeDelegateModel public: NumberDisplayDataModel(); - ~NumberDisplayDataModel() = default; + ~NumberDisplayDataModel(); public: QString caption() const override { return QStringLiteral("Result"); } @@ -50,5 +51,5 @@ class NumberDisplayDataModel : public NodeDelegateModel private: std::shared_ptr _numberData; - QLabel *_label; + QPointer _label; }; diff --git a/examples/calculator/NumberSourceDataModel.cpp b/examples/calculator/NumberSourceDataModel.cpp index f6a7ca55d..153e006cd 100644 --- a/examples/calculator/NumberSourceDataModel.cpp +++ b/examples/calculator/NumberSourceDataModel.cpp @@ -11,6 +11,11 @@ NumberSourceDataModel::NumberSourceDataModel() , _number(std::make_shared(0.0)) {} +NumberSourceDataModel::~NumberSourceDataModel() +{ + delete _lineEdit; +} + QJsonObject NumberSourceDataModel::save() const { QJsonObject modelJson = NodeDelegateModel::save(); diff --git a/examples/calculator/NumberSourceDataModel.hpp b/examples/calculator/NumberSourceDataModel.hpp index 8ae4e324a..ab0765dbc 100644 --- a/examples/calculator/NumberSourceDataModel.hpp +++ b/examples/calculator/NumberSourceDataModel.hpp @@ -3,6 +3,7 @@ #include #include +#include #include @@ -25,7 +26,7 @@ class NumberSourceDataModel : public NodeDelegateModel public: NumberSourceDataModel(); - virtual ~NumberSourceDataModel() {} + virtual ~NumberSourceDataModel(); public: QString caption() const override { return QStringLiteral("Number Source"); } @@ -60,5 +61,5 @@ private Q_SLOTS: private: std::shared_ptr _number; - QLineEdit *_lineEdit; + QPointer _lineEdit; }; diff --git a/examples/node_validation/TextDisplayModel.hpp b/examples/node_validation/TextDisplayModel.hpp index 7b77c282d..18a33b353 100644 --- a/examples/node_validation/TextDisplayModel.hpp +++ b/examples/node_validation/TextDisplayModel.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include "TextData.hpp" @@ -23,6 +24,8 @@ class TextDisplayModel : public NodeDelegateModel : _label(nullptr) {} + ~TextDisplayModel() override { delete _label; } + QString caption() const override { return QStringLiteral("Text Display"); } QString name() const override { return QStringLiteral("TextDisplay"); } @@ -66,5 +69,5 @@ class TextDisplayModel : public NodeDelegateModel } private: - QLabel *_label; + QPointer _label; }; diff --git a/examples/node_validation/TextSourceModel.hpp b/examples/node_validation/TextSourceModel.hpp index 3adb5a850..2c002cff0 100644 --- a/examples/node_validation/TextSourceModel.hpp +++ b/examples/node_validation/TextSourceModel.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include "TextData.hpp" @@ -23,6 +24,8 @@ class TextSourceModel : public NodeDelegateModel : _lineEdit(nullptr) {} + ~TextSourceModel() override { delete _lineEdit; } + QString caption() const override { return QStringLiteral("Text Source"); } QString name() const override { return QStringLiteral("TextSource"); } @@ -65,5 +68,5 @@ private Q_SLOTS: private: std::shared_ptr _data; - QLineEdit *_lineEdit; + QPointer _lineEdit; }; diff --git a/examples/node_validation/ValidatedModel.hpp b/examples/node_validation/ValidatedModel.hpp index 1c702fbcf..11c602392 100644 --- a/examples/node_validation/ValidatedModel.hpp +++ b/examples/node_validation/ValidatedModel.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -31,6 +32,9 @@ class ValidatedModel : public NodeDelegateModel , _minLength(3) {} + // The label is a child of the widget, so deleting the widget deletes it. + ~ValidatedModel() override { delete _widget; } + QString caption() const override { return QStringLiteral("Text Validator"); } QString name() const override { return QStringLiteral("TextValidator"); } @@ -152,7 +156,7 @@ class ValidatedModel : public NodeDelegateModel private: std::shared_ptr _outputData; - QWidget *_widget; - QLabel *_label; + QPointer _widget; + QPointer _label; int _minLength; }; diff --git a/examples/resizable_images/ImageLoaderModel.hpp b/examples/resizable_images/ImageLoaderModel.hpp index 394cf28a9..29b70d17c 100644 --- a/examples/resizable_images/ImageLoaderModel.hpp +++ b/examples/resizable_images/ImageLoaderModel.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -25,7 +26,7 @@ class ImageLoaderModel : public NodeDelegateModel public: ImageLoaderModel(); - ~ImageLoaderModel() = default; + ~ImageLoaderModel() override { delete _label; } public: QString caption() const override { return QString("Image Source"); } @@ -51,7 +52,7 @@ class ImageLoaderModel : public NodeDelegateModel bool eventFilter(QObject *object, QEvent *event) override; private: - QLabel *_label; + QPointer _label; QPixmap _pixmap; }; diff --git a/examples/resizable_images/ImageShowModel.hpp b/examples/resizable_images/ImageShowModel.hpp index 7e4c8a06c..e9c3b5919 100644 --- a/examples/resizable_images/ImageShowModel.hpp +++ b/examples/resizable_images/ImageShowModel.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -23,7 +24,7 @@ class ImageShowModel : public NodeDelegateModel public: ImageShowModel(); - ~ImageShowModel() = default; + ~ImageShowModel() override { delete _label; } public: QString caption() const override { return QString("Image Display"); } @@ -49,7 +50,7 @@ class ImageShowModel : public NodeDelegateModel bool eventFilter(QObject *object, QEvent *event) override; private: - QLabel *_label; + QPointer _label; std::shared_ptr _nodeData; }; diff --git a/examples/text/TextDisplayDataModel.hpp b/examples/text/TextDisplayDataModel.hpp index 73c53e99c..1989c5d83 100644 --- a/examples/text/TextDisplayDataModel.hpp +++ b/examples/text/TextDisplayDataModel.hpp @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -25,7 +26,7 @@ class TextDisplayDataModel : public NodeDelegateModel public: TextDisplayDataModel(); - virtual ~TextDisplayDataModel() {} + virtual ~TextDisplayDataModel() { delete _label; } public: QString caption() const override { return QString("Text Display"); } @@ -48,6 +49,6 @@ class TextDisplayDataModel : public NodeDelegateModel QWidget *embeddedWidget() override { return _label; } private: - QLabel *_label; + QPointer _label; QString _inputText; }; diff --git a/examples/text/TextSourceDataModel.cpp b/examples/text/TextSourceDataModel.cpp index 244da4011..bd78367a4 100644 --- a/examples/text/TextSourceDataModel.cpp +++ b/examples/text/TextSourceDataModel.cpp @@ -8,6 +8,11 @@ TextSourceDataModel::TextSourceDataModel() // } +TextSourceDataModel::~TextSourceDataModel() +{ + delete _lineEdit; +} + unsigned int TextSourceDataModel::nPorts(PortType portType) const { unsigned int result = 1; diff --git a/examples/text/TextSourceDataModel.hpp b/examples/text/TextSourceDataModel.hpp index 8e5ca69a8..aaae002c3 100644 --- a/examples/text/TextSourceDataModel.hpp +++ b/examples/text/TextSourceDataModel.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include "TextData.hpp" @@ -24,6 +25,8 @@ class TextSourceDataModel : public NodeDelegateModel public: TextSourceDataModel(); + ~TextSourceDataModel() override; + public: QString caption() const override { return QString("Text Source"); } @@ -49,5 +52,5 @@ private Q_SLOTS: void onTextEdited(QString const &string); private: - QLineEdit *_lineEdit; + QPointer _lineEdit; }; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 319351db8..47b743126 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -16,6 +16,7 @@ add_executable(test_nodes src/TestBasicGraphicsScene.cpp src/TestNodeGroup.cpp src/TestNodeNickname.cpp + src/TestEmbeddedWidgetOwnership.cpp src/TestUIInteraction.cpp src/TestNodeDrag.cpp src/TestDataFlow.cpp diff --git a/test/src/TestEmbeddedWidgetOwnership.cpp b/test/src/TestEmbeddedWidgetOwnership.cpp new file mode 100644 index 000000000..422797ff0 --- /dev/null +++ b/test/src/TestEmbeddedWidgetOwnership.cpp @@ -0,0 +1,152 @@ +#include "ApplicationSetup.hpp" + +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +#include +#include + +using QtNodes::DataFlowGraphicsScene; +using QtNodes::DataFlowGraphModel; +using QtNodes::NodeData; +using QtNodes::NodeDataType; +using QtNodes::NodeDelegateModel; +using QtNodes::NodeDelegateModelRegistry; +using QtNodes::NodeId; +using QtNodes::PortIndex; +using QtNodes::PortType; + +namespace { + +// Every widget made by EagerWidgetModel, in creation order. +std::vector> &createdWidgets() +{ + static std::vector> widgets; + return widgets; +} + +// A model that creates its widget in the constructor. This is the pattern +// that leaked, because the registry makes a temporary model. +// The model keeps the widget in a QPointer and deletes it in the destructor. +class EagerWidgetModel : public NodeDelegateModel +{ +public: + EagerWidgetModel() + : _widget(new QLineEdit()) + { + createdWidgets().push_back(_widget.data()); + } + + ~EagerWidgetModel() override { delete _widget; } + + QString name() const override { return QStringLiteral("EagerWidgetModel"); } + QString caption() const override { return QStringLiteral("Eager"); } + + unsigned int nPorts(PortType) const override { return 0; } + NodeDataType dataType(PortType, PortIndex) const override { return {}; } + void setInData(std::shared_ptr, PortIndex) override {} + std::shared_ptr outData(PortIndex) override { return nullptr; } + + QWidget *embeddedWidget() override { return _widget; } + +private: + QPointer _widget; +}; + +std::shared_ptr makeRegistry() +{ + auto registry = std::make_shared(); + registry->registerModel(); + return registry; +} + +int aliveWidgets() +{ + int count = 0; + for (auto const &w : createdWidgets()) + if (w) + ++count; + return count; +} + +} // namespace + +TEST_CASE("A widget made by the temporary registry model is deleted with that model", + "[widget-ownership]") +{ + auto app = applicationSetup(); + createdWidgets().clear(); + + auto registry = makeRegistry(); + + // Registration made exactly one temporary model, and it is already gone. + REQUIRE(createdWidgets().size() == 1); + CHECK(aliveWidgets() == 0); +} + +TEST_CASE("A model that deletes its widget in the destructor is safe in a scene", + "[widget-ownership]") +{ + auto app = applicationSetup(); + createdWidgets().clear(); + + auto registry = makeRegistry(); + createdWidgets().clear(); + + SECTION("Deleting the node frees its widget once") + { + DataFlowGraphModel model(registry); + DataFlowGraphicsScene scene(model); + + NodeId const id = model.addNode(QStringLiteral("EagerWidgetModel")); + REQUIRE(createdWidgets().size() == 1); + CHECK(aliveWidgets() == 1); + + CHECK(model.deleteNode(id)); + CHECK(aliveWidgets() == 0); + } + + SECTION("Undo of a delete makes a new widget and the old one stays freed") + { + DataFlowGraphModel model(registry); + DataFlowGraphicsScene scene(model); + + NodeId const id = model.addNode(QStringLiteral("EagerWidgetModel")); + scene.nodeGraphicsObject(id)->setSelected(true); + + scene.onDeleteSelectedObjects(); + REQUIRE(createdWidgets().size() == 1); + CHECK(aliveWidgets() == 0); + + scene.undoStack().undo(); + REQUIRE(createdWidgets().size() == 2); + CHECK(aliveWidgets() == 1); + + scene.undoStack().redo(); + CHECK(aliveWidgets() == 0); + } + + SECTION("Destroying the scene before the model frees the widget once") + { + DataFlowGraphModel model(registry); + { + DataFlowGraphicsScene scene(model); + model.addNode(QStringLiteral("EagerWidgetModel")); + CHECK(aliveWidgets() == 1); + } + // The scene is gone. The proxy widget deleted the widget with it. + // The model destructor must not delete it a second time. + } + + CHECK(aliveWidgets() == 0); +}