Qt(windows下)捕获异常信息并自动重启

参考 https://blog.csdn.net/x85371169/article/details/79267592

目前在弄一个工业上用的软件,需要实现无人值守功能。软件经过两三个星期的debug,已经将绝大部分导致软件死机的bug修复,但是由于能力有限,软件还是会在某些情况下(我也不知道啥情况。。)死机,因此只能弄个错误捕获,然后自动重启了。

windows下的异常捕获过程可以参考文章开头那个博文。但是那篇文章中的QMessageBox,会有无法点击的问题(假如异常是在子线程触发的)。
下面的代码是对该情况的修复,顺便增加了个重启功能。

#include <DbgHelp.h>
QMessageBox *msgbPtr;//在子线程内创建的messageBox会有问题,所以只能在主线程创建并传递过过来
void initMessageBox()
{
    msgbPtr = new QMessageBox();
    msgbPtr->setWindowFlag(Qt::WindowStaysOnTopHint);
    msgbPtr->setIcon(QMessageBox::Critical);
    msgbPtr->setWindowTitle("软件崩溃");
    msgbPtr->setText("未知原因崩溃,程序将在5s后重启");

    QPushButton *rebootButton;
    QPushButton *rejectButton ;

    rebootButton = msgbPtr->addButton("重启", QMessageBox::AcceptRole);
    rejectButton = msgbPtr->addButton("退出", QMessageBox::RejectRole);

    QObject::connect(msgbPtr, &QMessageBox::buttonClicked, [=](QAbstractButton *button){
        if(msgbPtr->clickedButton() == (QAbstractButton*)rebootButton)
        {
            qDebug() << "start application:" << QProcess::startDetached(qApp->applicationFilePath(), QStringList());//重启
//            qApp->quit();
        }

        msgbPtr->close();
    });
}

//异常捕获回调函数
long __stdcall   errCallback(_EXCEPTION_POINTERS*  pException)
{
/******这里最好还是加个防止反复进入该回调函数的机制。防止在倒计时的过程中,程序那边又再次触发异常。*********/
    // 信号量的意义,把操作共享内存的代码锁住。因为有可能同时启动, 防止并发
    QSystemSemaphore sema("my exception", 1, QSystemSemaphore::Open);
    sema.acquire();
 
    QDir dir;
    dir.mkdir("../dumps");
    dir.cd("../dumps");

    /*
      ***保存数据代码***
    */

    QString fileName = dir.path() + "/" +
                       QDateTime::currentDateTime().toString("yyyyMMdd_HHmmss.zzz") + ".dmp";

    LPCWSTR pFileName = (LPCWSTR)fileName.unicode();

    //创建 Dump 文件
    HANDLE hDumpFile = CreateFile(pFileName,
                                  GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    qDebug() << "create dumpFile:" << hDumpFile << INVALID_HANDLE_VALUE;
    if(hDumpFile != INVALID_HANDLE_VALUE)
    {
        //Dump信息
        MINIDUMP_EXCEPTION_INFORMATION dumpInfo;
        dumpInfo.ExceptionPointers = pException;
        dumpInfo.ThreadId = GetCurrentThreadId();
        dumpInfo.ClientPointers = TRUE;
        //写入Dump文件内容
        MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpNormal, &dumpInfo, NULL, NULL);
    }

    EXCEPTION_RECORD* record = pException->ExceptionRecord;
    QString errCode(QString::number(record->ExceptionCode,16)),errAdr(QString::number((uint)record->ExceptionAddress,16)),errMod;


    qDebug() << "main thread:" << qApp->thread() << QThread::currentThread();

    int lastTime = 5;
    QTimer *timer = new QTimer();
    timer->setInterval(1000);
    QObject::connect(timer, &QTimer::timeout, [&lastTime, &errCode](){
        lastTime--;

        if(lastTime < 0)
        {
            //            qApp->quit();
            qDebug() << "start application:" << QProcess::startDetached(qApp->applicationFilePath(), QStringList());//重启
            msgbPtr->close();
            return;
        }

        QString info = QString("未知原因崩溃,程序将在%1s后重启\r\n%2").arg(lastTime).arg(errCode);
        msgbPtr->setText(info);
    });
    timer->start();

    msgbPtr->exec();


    return EXCEPTION_EXECUTE_HANDLER;
}

然后在main函数中执行

int main(int argc, char *argv[])
{
    SetUnhandledExceptionFilter(errCallback);
    QApplication app(argc, argv);
    initMessageBox();

    .....
    
}
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值