參考 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)
{
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();
.....
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/224854.html
標籤:其他
上一篇:2020-11-17
