Originally Posted by
d_stranz
The usual methods are:
- Use try/catch clauses around areas of the code that could throw an exception, making sure your catch() clauses include the right exceptions
- Add a signal handler. There is an example here.
Generally a crash occurs due to a segmentation fault (
SIGSEGV) because your code has made an invalid memory access. You usually cannot fix such a crash at runtime, because once memory is corrupted, there isn't much you can do except halt the program.
Whether you will be able to display a QMessageBox or not depends on how broken your program is. If the code has corrupted some critical section needed by Qt's event loop or other functions, trying to display a message might just cause more faults.
Yep. Learn how to program defensively:
- Check all return values from functions that return some kind of status that indicates whether they succeeded or not
- Check any pointers for valid values (eg. non-null) before you use them, especially pointers returned from function calls. Don't assume that when a method returns a pointer you can just use it without checking.
- Check array indexes before using them to make sure you don't try to access memory that is out of range of your array
- Add assert() statements to your code to check that variables, pointers, etc. have valid values.
- Always, always make a debug version of your program and run it in the debugger. Learn to use the debugger output to backtrack where the error occurred and fix it.
- If you are writing code that others will use (i.e. a library), document the methods in your classes to help others avoid mistakes. In particular, if methods must be called in a certain order or variables initialized, make sure that is clear in your comments.
Thank you very much! I found out how to handle the crash, but I need some help with displaying the QMessageBox after app closes. How can I achieve that?
My code:
#include <QApplication>
#include "mainwindow.h"
#include <QMessageBox>
#include <signal.h>
void signalHandler(int signum){
signal(signum, SIG_DFL);
qDebug() << "App crashed";
QMessageBox::critical(nullptr,
"App crashed",
"Application has unexpectedly close! Try restarting the application.");
}
int main(int argl,char *argv[])
{
Q_INIT_RESOURCE(resources);
signal(SIGSEGV, signalHandler);
mainWindow *window = new mainWindow();
window->setWindowTitle("Test");
window->setFixedSize(700, 400);
window->show();
return app.exec();
}
#include <QApplication>
#include "mainwindow.h"
#include <QMessageBox>
#include <signal.h>
void signalHandler(int signum){
signal(signum, SIG_DFL);
qDebug() << "App crashed";
QMessageBox::critical(nullptr, "App crashed", "Application has unexpectedly close! Try restarting the application.");
}
int main(int argl,char *argv[])
{
Q_INIT_RESOURCE(resources);
signal(SIGSEGV, signalHandler);
QApplication app(argl,argv);
mainWindow *window = new mainWindow();
window->setWindowTitle("Test");
window->setFixedSize(700, 400);
window->show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
In my code, the QMessageBox appears first, and then the app is closed. I want to close the mainWindow or making it dissapear and the displaying the QMessageBox.
Bookmarks