Skip to content

A possible leaking memory problem #248

Closed

Description

@JesusCRIS90

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

  1. paceholder commented on Nov 22, 2026

    @paceholder
    Owner

    Thanks for the detailed report. Your analysis is right, and we reproduced it.

    What goes wrong

    • The registry builds a temporary model to read its name. A widget created in the constructor of that model is never embedded, so it leaks. Only the model can free it.
    • After a node embeds the widget, the node's QGraphicsProxyWidget owns it and deletes it with the node. A plain delete in 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

    class MyModel : public NodeDelegateModel
    {
    public:
        ~MyModel() override { delete _widget; }          // 2
    
        QWidget *embeddedWidget() override
        {
            if (!_widget)                                // 1
                _widget = new QLineEdit();
            return _widget;
        }
    
    private:
        QPointer<QWidget> _widget;                       // 3
    };
    1. Create the widget lazily in embeddedWidget(). The temporary model never calls it, so it makes no widget.
    2. Delete the widget in the destructor. This is what frees a widget that was never embedded.
    3. Keep it 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, delete _widget does 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 delete leaks, and a raw pointer with delete crashes.

    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.

  2. added a commit that references this issue on Nov 22, 2026
    1e547da
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions