• 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(! | QIODevice::Text)){
    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);
    case QtWarningMsg:

    tsTextStream << QString("myProjectLogWarning%1: %2 %3 %4 %5\n").arg(sCurrDateTime).arg(msg).arg(context.file).arg(context.line).arg(context.function);
    case QtCriticalMsg:

    tsTextStream << QString("myProjectLogCritical%1: %2 %3 %4 %5\n").arg(sCurrDateTime).arg(msg).arg(context.file).arg(context.line).arg(context.function);
    case QtFatalMsg:

    tsTextStream << QString("myProjectLogFatal%1: %2 %3 %4 %5\n").arg(sCurrDateTime).arg(msg).arg(context.file).arg(context.line).arg(context.function);

    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!

  • Qt Champions 2017

    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":
    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":

  • Lifetime Qt Champion


    One thing that might also comes into play, your custom message handler doesn't have any protection for concurrent access

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.