PDA

View Full Version : Application not exiting gracefully if I want close to in CMainWindow ctor



rawfool
27th July 2013, 12:22
In QMainWindow, I'm initializing a CWorker class and this CWorker tries to find if a particular shared memory is available. If shared memory isn't available then I should quit the application (i.e., QMainWindow).
I'm trying to check this in try.. catch. So if the control goes to catch, I'm trying to display a QMessageBox & exit(this->close) the application. While doing this, the application is not closing gracefully.



CMainWindow::CMainWindow(QWidget *parent)
: QMainWindow(parent)
{
....
....
try
{
worker = new CWorker();
}
catch(...)
{
QMessageBox::information(this, "Title",
"Message text goes here");
quit();
}

....
....
....
}


I tried qApp->quit() and QCoreApplication::quit(); also but same result;

Please find the attached screenshot of the error message after the application goes to catch().

9372

wysota
27th July 2013, 14:43
At the time when constructors are called, the event loop is not running yet, thus trying to quit it is a no-op.

Radek
28th July 2013, 09:59
Add a dummy statement behind the new and set breaks at new and in the catch block. Debug. This way, you will localize the problem. If it is the quit() (GP fault or the app tries to continue normally) then you'd better rethrow from the CMainWindow ctor and put try - catch in your main(). You should end in the catch block here. You can exit() simply because all objects created in the try block of the main() are already destructed.