Cathing SEGFAULT
-
Hello. I have a multithread application. It works fine on many computers but it often crashes on some of them. So I can't find the reason cause it works ok on my own computer and I can't find what leads to SEGFAULT. I decided to log. I found this example
@void myMessageOutput(QtMsgType type,const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
QFile fMessFile(qApp->applicationDirPath() + "/myProjectLog.log");
if(!fMessFile.open(QIODevice::Append | QIODevice::Text)){
return;
}
QString sCurrDateTime = "[" + QDateTime::currentDateTime().toString("dd.MM.yyyy hh:mm:ss.zzz") + "]";
QTextStream tsTextStream(&fMessFile);
switch (type) {
case QtDebugMsg:tsTextStream << QString("myProjectLogDebug%1: %2 %3 %4 %5\n").arg(sCurrDateTime).arg(msg).arg(context.file).arg(context.line).arg(context.function);
break;
case QtWarningMsg:tsTextStream << QString("myProjectLogWarning%1: %2 %3 %4 %5\n").arg(sCurrDateTime).arg(msg).arg(context.file).arg(context.line).arg(context.function);
break;
case QtCriticalMsg:tsTextStream << QString("myProjectLogCritical%1: %2 %3 %4 %5\n").arg(sCurrDateTime).arg(msg).arg(context.file).arg(context.line).arg(context.function);
break;
case QtFatalMsg:tsTextStream << QString("myProjectLogFatal%1: %2 %3 %4 %5\n").arg(sCurrDateTime).arg(msg).arg(context.file).arg(context.line).arg(context.function);
abort();
}
}@It works perfect with debug messages and I can see them in the log file. But when I call SEGFAULT intentionally to check if the function works as I need, I can see nothing in my log file and I don't get to the breakpoint at line 23. Is there any way to catch SEGFAULT under Windows. I found some other ways only for Linux or MacOS. Or maybe I should do smth completely different to see where my application crashes. Thanx a lot!
-
There is not easy to way to identify the segmentation fault. When it fails you can't expect the code to come to your message handler. You can try with C++ exception handling. Still identifying the location of fault is not deterministic.
-
You may try to use "segvcatch library":https://code.google.com/p/segvcatch/
It converts hardware exceptions into c++ exceptions -
Thanx a lot! But I read it is not good to use try-catch in Qt, isn't it?
-
I think that for your use case it is ok to use try-catch in the main function like it is described "here":http://qt-project.org/doc/qt-5/exceptionsafety.html#recovering-from-exceptions
-
Hi,
One thing that might also comes into play, your custom message handler doesn't have any protection for concurrent access