will destroyed() be emitted if the constructor of a class derived from QObject throws?

Posted by PorkyBrain on Stack Overflow See other posts from Stack Overflow or by PorkyBrain
Published on 2012-11-16T22:50:05Z Indexed on 2012/11/16 22:59 UTC
Read the original article Hit count: 166

Filed under:
|
|

Ive seen Qt GUI syntax like the following all over the place:

myDialog::myDialog(QWidget *parent, Qt::WFlags flags):QDialog(parent, flags)
{
    QPushButton *button = new QPushButton("&Download", this);
    QVBoxLayout *layout = new QVBoxLayout(this);
    //something that can throw here
    layout ->addWidget(button );
    setLayout(layout);
}

I've always wondered if this can leak in the event of an exception because the "this" I'm giving as a parent to button and layout is not fully constructed so it might not destroy its children.

I tried it out in MSVC2010 Qt4.8.3 and it looks like as soon as the base QObject class is fully created (which is done first of course) it is ok to pass "this" to other objects in the constructor, they will destroyed correctly.

I haven't found the spot in the Qt docs guaranteeing this though, can someone point me to it so I have assurance that this will not change in the future?

© Stack Overflow or respective owner

Related posts about c++

Related posts about qt