Important: Please read the Qt Code of Conduct -

Doubt about deleting a child QObject

  • Hi all,
    imagine I've got class A that inherits QObject and that creates an instance of class B, which inherits from QObject too, attaching himself as parent, so that B (instance) has A (instance) as parent. Now imagine that A performs an explicit delete on B (instance). What happens when A is deleted too? I mean, could the explicit deletion cause some sort of problems or the Qt is simply passing thru the no-more valid B pointer/reference? I imagine the latter, but it is just to be sure.

  • No, there is no problem in the situation you described, the child delete itself from it's parent's list of children.

    There is an issue if the child is allocated on stack and the parent gets deleted first: in this case parent calls delete on an address of a stack allocated object.

    //see "this": doc page

Log in to reply