Repository navigation
Description
Trying to implement another cause of use, a block with a Custom Qwidget, I have found a memory problem that it is present in all the examples.
In the examples when a new block(Node) is created, a custom destructor is created. This destructor normally is empty. So nobody frees the Qwidget pointer. The curious here is that this problem is only present at the beginning when we run the application.
I have verified that when the scene is created, also an instance of each Node that we have set in the function “registerDataModel()” is created but immediately these instances are deleted. Here is the leaking problem, the Qwidget pointer of each node is not free.
If we try to solve this problem adding in the destructor adding the correspondent code “delete Qwidget”, we solve the problem but another worse problem it is created, a CRASH is produced when we delete (key Supr) a Node manually in our scene. This crash is due to that when we push the key Supr to delete a Node in the scene, first is deleted the pointer to the Qwidget and after the destructor of the Node, it is called. When the destructor tries to delete the pointer to the Qwidget, a crash in the application is produced because it is trying to delete a pointer(QWidget*) that already have been deleted.
I have found a way to solve both problems, but it is not the best solution. I have created a destructor with memory. The code for each destructor is the following:
`virtual ~CounterModel() override {
static int amount = 0;
if( amount == 0 )
{
delete _window;
_window = nullptr;
amount++;
}
else {
_window = nullptr;
}
} `
`
I think that this could be fixed in two ways:
1. When the keyword “Supr” is pressed, only delete the Node(Model), so the same model will make the action of cleaning the pointer of Qwidget.
2. Make an overload function as follow:
std::shared_ptr<QWidget*> embeddedWidget() override { return _window;}
Here an link to a video to explain better the error:
https://www.dropbox.com/s/wbdn8ooghyhoglc/Example?dl=0
Also it is possible that I am doing something wrong… In that case sorry for the inconvenience.
Activity
Scene is not cleared if operation is cancelled during a loadScene() (…
Scene is not cleared if operation is canceled during a loadScene() (p…
paceholder commentedon Nov 22, 2026
Thanks for the detailed report. Your analysis is right, and we reproduced it.
What goes wrong
QGraphicsProxyWidgetowns it and deletes it with the node. A plaindeletein the model destructor then deletes it a second time. That was your crash. It happens when the scene is destroyed before the model.So there are two possible owners, and which one dies first depends on the path (delete key, undo, closing the window). A raw pointer cannot tell whether the widget is still alive.
The fix has three parts
embeddedWidget(). The temporary model never calls it, so it makes no widget.QPointer. AQPointerdoes not delete anything. It follows theQObjectand becomes null when the object is destroyed, whoever destroys it. If the proxy has already deleted the widget,delete _widgetdoes nothing. With a raw pointer it would delete the widget twice and crash.We checked this with tests that fail with both old patterns: a raw pointer without
deleteleaks, and a raw pointer withdeletecrashes.PR #547 updates all examples to this pattern and adds a "Widget ownership" section to the data-flow guide. It will close this issue when it is merged.
Delete embedded widgets in the model destructor, through a QPointer