How to catch the crash of the Qt program



  • Hi all:
    i am developing a tool which running in the windows platfoem (maybe later will porting to other platforms).
    here i face a problem, when the tool released, but the tool crashed unexpected. So is there any possibility to catch the crash/exception, save the exception memories, and generate the symbols (in the Qt build toolchain), then I can use the windows debugger to open the symbol and the memory file to dump the callstack , or is there any posibility to get the crash function and line, even the callstack directly.

    Here is my current implementation which copied from the internet, that can only catch the exception, and save some memories. but i do not know how to create symbols for release/debug mode.

    LONG ApplicationCrashHandler(EXCEPTION_POINTERS *pException)
    {
        //And output crash information
        EXCEPTION_RECORD *record = pException->ExceptionRecord;
        QString errCode("0x" + QString::number(record->ExceptionCode, 16));
        QString errAddr("0x" + QString::number((uint)record->ExceptionAddress, 16));
        //QString errFlag("0x" + QString::number(record->ExceptionFlags, 16));
        //QString errPara("0x" + QString::number(record->NumberParameters, 16));
    
        QString str;
    
        str.append("Sorry ! Crash Happen\r\n\r\n");
        str.append("Error Code        : " + errCode + "\r\n");
        str.append("Error Address   : " + errAddr + "\r\n");
    
        QMessageBox::critical(NULL, "Crash Happen", str, QMessageBox::Close);
    #if 0
        // here is to save the memories currenlty.
        QString dumpfilename = "../crash.dump";
    
        HANDLE hDumpFile = CreateFile((LPCWSTR)dumpfilename.utf16(),
                 GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    
        if(hDumpFile != INVALID_HANDLE_VALUE) {
            qDebug() << "Create File Succeed";
            MINIDUMP_EXCEPTION_INFORMATION dumpInfo;
            dumpInfo.ExceptionPointers = pException;
            dumpInfo.ThreadId = GetCurrentThreadId();
            dumpInfo.ClientPointers = TRUE;
            MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(),
                              hDumpFile, MiniDumpNormal, &dumpInfo, NULL, NULL);
            CloseHandle(hDumpFile);
        } else {
            qDebug() << "Create File Failed";
        }
    #endif
        return EXCEPTION_EXECUTE_HANDLER;
    }
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)ApplicationCrashHandler);
    
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    
    
    

    the source code are in the main.cpp.


Log in to reply
 

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