Results 1 to 9 of 9

Thread: Detect the application getting crash or normal exit when application exit

  1. #1
    Join Date
    Dec 2016
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Question Detect the application getting crash or normal exit when application exit

    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.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Detect the application getting crash or normal exit when application exit

    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. 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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Dec 2016
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Question Re: Detect the application getting crash or normal exit when application exit

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Detect the application getting crash or normal exit when application exit

    Hi,
    As d_stranz pointed you, is it better to catch this error and do not let it happen
    Qt Code:
    1. QString data;
    2. if (list.count() > 0)
    3. data = list.at(0);
    To copy to clipboard, switch view to plain text mode 
    Would be much better. But as you declare "list" as an empty QStringList, "data = list.at(0);" will never be executed.
    Òscar Llarch i Galán

  5. #5
    Join Date
    Dec 2016
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: Detect the application getting crash or normal exit when application exit

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Detect the application getting crash or normal exit when application exit

    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,
    _

  7. #7
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Detect the application getting crash or normal exit when application exit

    Hi,

    Instead of crashing, i need to show a error message for this case
    Do you know the "else" statement?
    Something like
    Qt Code:
    1. if (list.count() > 0)
    2. data = list.at(0);
    3. else
    4. //Show an alert message here!
    To copy to clipboard, switch view to plain text mode 
    Òscar Llarch i Galán

  8. #8
    Join Date
    Dec 2016
    Posts
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Detect the application getting crash or normal exit when application exit

    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.

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Detect the application getting crash or normal exit when application exit

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Application crashing at exit(0).
    By adore in forum Qt Programming
    Replies: 1
    Last Post: 14th May 2015, 13:19
  2. How to exit Qt console application?
    By arcelivez in forum Qt Programming
    Replies: 14
    Last Post: 30th July 2014, 09:51
  3. Replies: 4
    Last Post: 19th November 2012, 15:35
  4. QML Application crashing on exit
    By fgungor in forum Qt Quick
    Replies: 0
    Last Post: 12th March 2011, 11:19
  5. Replies: 2
    Last Post: 21st November 2010, 19:03

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.