Is it possible QApplication to start deleting children before a particular object has been deleted using deleteLater?

  • This is going to sound a little confusing and obviously not good practice (I discovered it when investigating a series of crashes in my Qt application). I'm seeing some crashing in my Qt application in my beta testing community, but the crash scenarios themselves are seemingly impossible to reproduce on my team's end.

    Assume that a QObject named _controller is parented by QApplication.
    The instance _controller has a member, QObject * named _child, that it also parents, e.g. _child.setParent(this), this is _controller.

    Shortly before the QApplication is exited, _controller calls deleteLater on _child, e.g. _child.deleteLater(). And shortly after that, QApplication is exited and begins deleting children (so it deletes _controller via deleteChildren()).

    Is it possible that the base destructor of _controller tries to delete the _child object before the effect of deleteLater()? Effectively the question is, is it possible for the QApplication destruction to beat the destruction of _child?

    I can't post any source code or stack traces as they're proprietary. But this seems like the only thing that could be causing the crash.

  • Lifetime Qt Champion


    If I may, making an object a child of QApplication is AFAIK pretty unusual.
    Generally you have the "main objects" on the stack (you can see it in almost all examples/demos) or if on the heap, either in a smart pointer like QScopedPointer or deleted manually before returning the result of app.exec()

  • Yes I realize this and I completely agree with you. I didn't write this code though and while I've corrected it to use the right approach, the crash isn't reproducible on my end, so it would be great to know whether or not what I'm thinking could be the cause. Otherwise I'll have to wait and observe the behavior in the beta field, with lesser confidence.

    Consider a single stack Application object in main(). And the controller object on the heap, with the controller object parented by Application. Since Application is a QObject, it will delete it's children on exit, when its destructor runs, and consequently so does the destructor for controller.

  • Lifetime Qt Champion

    Are you running your program on the same OS/Architecture/Compiler as your users ?

    By the way which Qt version are you using ?

    I have never tried this that way so i can't tell you 100% whether you're right or not. Might be a double free.

    Since it concerns a pretty low-level problem, you should also ask on the interest mailing list, you'll find Qt's developers/maintainers there (this forum is more user oriented)

Log in to reply