Qt应用程序异常崩溃该怎么办,生成dump文件再回溯分析,可以快速且准确的帮助我们定位到崩溃的点。那么,本章我们分享下如何在Qt中生成dump文件。
一、使用minudump捕获崩溃信息
#include <QCoreApplication>
#include <QDir>
#include <QDateTime>
#include <QDebug>
#ifdef Q_OS_WIN
# include <windows.h>
# include <dbghelp.h>
# pragma comment( lib, "Dbghelp.lib" )
#endif
#define DumpFileDir qApp->applicationDirPath() + "/dump/"static LONG WINAPI AppExceptionCallback(struct _EXCEPTION_POINTERS *ExceptionInfo)
{QString savePath = DumpFileDir;savePath.append(QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz"));savePath.append(".dmp");qDebug() << savePath;QString msg;HANDLE hDumpFile = CreateFileW(savePath.toStdWString().c_str(),GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ,0, CREATE_ALWAYS, 0, 0);if(INVALID_HANDLE_VALUE == hDumpFile){return EXCEPTION_EXECUTE_HANDLER;}MINIDUMP_EXCEPTION_INFORMATION ExpParam;ExpParam.ThreadId = GetCurrentThreadId();ExpParam.ExceptionPointers = ExceptionInfo;ExpParam.ClientPointers = TRUE;MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile,MiniDumpWithDataSegs, ExceptionInfo ? &ExpParam : nullptr, nullptr, nullptr);CloseHandle(hDumpFile);QString strTitle ("Application Error");QString strContent ("I'm Sorry,Application is Crash!");qDebug() << strContent << "," << QStringLiteral("dump文件路径为 ") << savePath;return EXCEPTION_EXECUTE_HANDLER;
}int main(int argc, char *argv[])
{QApplication app(argc, argv);#ifdef Q_OS_WIN /// 创建dumpQDir dir(DumpFileDir);if(!dir.exists()){if (!dir.mkpath(DumpFileDir))qDebug() << QStringLiteral("创建dump文件目录失败");}SetUnhandledExceptionFilter(AppExceptionCallback);
#endifreturn app.exec();
}
在pro中我们需要添加如下信息:
# 在release模式下生成debug信息:.pdb
QMAKE_CXXFLAGS_RELEASE = $$QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO
QMAKE_LFLAGS_RELEASE = $$QMAKE_LFLAGS_RELEASE_WITH_DEBUGINFO
二、准备好windbg工具
具体怎么操作呢,我们会在接下的章节中逐步深入!~