Important: Please read the Qt Code of Conduct -

Child/Parent QObject Cleanup

  • I'm having some troubles with segfaults when I delete this one QObject subclass. None of the classes subclass eachother, the only subclass QObject. Here is the hierarchy:
    Let's say that I have objects, A, B and C. B & C are children of A:


    • B
    • C

    But I also have some other objects (we'll call them X) that are the children of B. The problem is that the deconstructor for X requires a resource from B (a function call). But B is completely deleted before it's children (the X objects) are. Therefore when X is deleted I get a segfault.

    What can I do to fix this?

  • Moderators

    How are you deleting your objects? Are you using delete or deleteLater? Seems odd this is happening since any delete on an object will delete all it's children before itself. At least with Qt parenting. If object X isn't a true child of B then that would cause your issue.

    I.e. if I have:

    QLabel *label = new QLabel();
    QLabel *child = new QLabel(label);
    delete label;
    // label should delete child by before it fully deconstructs.

    Also if you have real code or at the very least a backtrace I could help more.