[Interest] App. is running after an exception is thrown.

Thiago Macieira thiago.macieira at intel.com
Fri Jul 26 23:17:46 CEST 2013


On sábado, 27 de julho de 2013 00:58:19, Constantin Makshin wrote:
> The exception prevents show() from finishing its work so your windows
> remains invisible. But it still exists (it wasn't shown, but nobody closed
> it either), so exec() happily runs the event loop, not knowing the
> application is in an unusable state.

Let me be clear: the application is in *undefined* state. The QtGui code is not 
exception safe, which means the exception unwind process did any number of bad 
things to the internal state and left things in possibly very bad conditions.

The correct version of the code is:
    QApplication a(argc, argv);
    event_dlg w;

    try
    {
        w.set_config_file("./events.xml");
    }
    catch ( std::runtime_error & e )
    {
//        QMessageBox msgBox;
//        msgBox.setText(e.what());
//        msgBox.exec();
//        qDebug() << w.close();
        return 1;
    }

    w.show();
    return a.exec();

There MUST NOT be any try { } block around w.show(). That function CANNOT 
throw, so do not make it throw against its wishes. If something causes it to 
throw by accident, your application should be allowed to crash, so it creates 
a core dump for you to analyse in the debugger. 

If you catch the exception, debugging will be harder. More importantly, if you 
catch the exception, you MUST NOT show a dialog. Do not use QtGui again. If an 
exception unwound the stack in QtGui, then QtGui is now in an undefined state, 
which means your application could do worse things than crash.


Also, please note that in Qt 5 an exception unwinding QtGui will cause a 
crash, no questions asked, before your catch handler gets called.
-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20130726/d1aaed67/attachment.sig>


More information about the Interest mailing list