PDA

View Full Version : Detect the application getting crash or normal exit when application exit



shahina
20th December 2016, 11:28
I need to restart the application when my application get crash. QProcess have finished() signal which gives the exit status as normal or crash. Like i need to get exit status for QWidget.

d_stranz
20th December 2016, 16:44
QWidget is not your application and does not have an "exit status". A class derived from QCoreApplication provides an exit status through the return value of QCoreApplication::exec(). It has a QCoreApplication::aboutToQuit() signal that is issued on a normal exit.

An application usually crashes due to some abnormal occurrence that your code does not trap. This is often the result of a signal (http://en.cppreference.com/w/c/program/SIG_types). You can install a signal handler to trap the signal, but that doesn't mean you can do anything about it other than make a more graceful exit from your program.

Why don't you find and fix the cause of your program crashes instead? Good programs never crash, because the programmers who write them take care to write code that checks for errors instead of just letting them happen.

shahina
21st December 2016, 06:30
yes QCoreApplication::aboutToQuit signal get emit , if it normally closed. But I need to handle the unexpected error or crash. Suppose consider the code as
In my main.cpp

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Widget w;
w.show();
return app.exec();
}

In Widget.cpp :

void Widget:: on_pushButton_clicked()
{
QStringList list;
QString data = list[0]; // index out of range
}
This will cause crash. I need to handle this error by catching the error. Instead of showing the crashing window and i need to show any error messge.

^NyAw^
21st December 2016, 08:47
Hi,
As d_stranz pointed you, is it better to catch this error and do not let it happen


QStringList list;
QString data;
if (list.count() > 0)
data = list.at(0);

Would be much better. But as you declare "list" as an empty QStringList, "data = list.at(0);" will never be executed.

shahina
21st December 2016, 09:58
Yes I know it gives run time while accessing the 0th item in list. My need is, i want to handle this type of error in application. Instead of crashing, i need to show a error message for this case. Thank you.

anda_skoa
21st December 2016, 10:15
In your original message you wrote that your need is to restart, which can easily be done with QProcess.

Now you write "instead of crashing".

The only way not to crash is not do things that make the program crash.
A crash is program state where the operating system can no longer execute the program, one cannot "recover" from a crash.

Cheers,
_

^NyAw^
21st December 2016, 11:51
Hi,



Instead of crashing, i need to show a error message for this case

Do you know the "else" statement?
Something like


if (list.count() > 0)
data = list.at(0);
else
//Show an alert message here!

Palsy
28th December 2016, 03:09
This doesn't make sense?

If it is crashing at line 2, then it will never reach the else.
You need to sanity check your list before it tries to assign from the array into data.

You should breakpoint before the crash occurs, and evaluate the list expression, then write some code to deal with that. (Unless I've misunderstood where the crash is occurring).

But if the question is what to show for a message box, use something like QMessageBox.

d_stranz
29th December 2016, 17:42
If it is crashing at line 2, then it will never reach the else.

The OP's code crashed because it was trying to access an entry from an empty list, so list.at( anything ) will fail. The code posted by NyAw checks to see that the list is not empty before trying to access the first element, so it should not crash. Even if the first element is empty, it will still return an empty QString for assignment into data. If the list itself is empty (count() == 0) then the else clause will be executed. That -is- the "sanity check".

If accessing a valid entry from a non-empty list could cause a crash (unlikely), then you can guard against that by enclosing the entire if /else conditional in an appropriate try / catch clause.