Important: Please read the Qt Code of Conduct -

Unexpected crash upon application exit

  • In an earlier question I asked about resource management with Qt, and in particular about the RAII principle (see:

    In one of the answers it was mentioned that in theory with Qt it is not necessary to think about this too much, because Qt internally keeps track of the lifetime of QObjects.

    I created a very small code snippet that seems to indicate the opposite:
    Running this snippet causes a nullptr-exception in qscopedpointer.h on line 112 (the line number is for Qt 5.6.0 beta, on hash 38e602d0f). Allthough I'm referring to Qt 5.6, I noticed the same problem already in Qt 5.4.

    Did I stumble on a subtle, yet important bug, or did I something 'illegal'?

  • Moderators

    Switch the order of the construction of objects. When the parent's destructor runs it'll try to free its children, but since you've created it after the child (hence its destructor will be called first), it will try to delete a stack object.