QT Program exception crash handling tips (Win)

Source: Internet
Author: User

This article is about the QT4 program's exception handling technique under the Windows system, so it needs to include "#include <Windows.h>" in the header file.

First of all, the program will inevitably have an abnormal crash, it is important to be in a crash in time to save important data, reduce the loss.

The SetUnhandledExceptionFilter function is an exception-capturing function of Win32API, which invokes the callback function registered by the function before the end of the program, so that the specified code can be executed before the process terminates, such as the ability to save the data.

exception_record* RECORD = pexception->exceptionrecord; QString Errcode (Qstring::number (record->exceptioncode,16)), Erradr (Qstring::number ((UINT) record->    exceptionaddress,16)), Errmod; Qmessagebox::critical (NULL, "program Crashes", "<font size=4><div><b> a sincere apology for the error that occurred </b><br/> </div> "+ QString (" <div> error code:%1</div><div> error Address:%2</div></font> "). Arg (Errcode    ). Arg (ERRADR), Qmessagebox::ok); return exception_execute_handler;}    int main (int argc, char *argv[]) {qapplication A (argc, argv);    QTEXTCODEC::SETCODECFORTR (Qtextcodec::codecforlocale ());    Qtextcodec::setcodecforcstrings (Qtextcodec::codecforlocale ());    Qtextcodec::setcodecforlocale (Qtextcodec::codecforlocale ());    SetUnhandledExceptionFilter ((lptop_level_exception_filter) applicationcrashhandler);//Register exception capture function MainWindow W; W. showmaximized (); return a.exec ();}

Saving data is only a salvage measure, and more importantly, finding the source of the error. If you can crash, the program automatically records the crash when the running information, will help to correct the work. Microsoft provides "DBGHELP" error debugging technology, call the relevant function to save the program crash information, and then with the help of WinDbg software can analyze the execution of the situation.

Use it to link the "dbghelp" library, call the MiniDumpWriteDump function to save the ". Dmp" suffix of the dump file, which can be WinDbg read and analyzed.

LONG Applicationcrashhandler (exception_pointers *pexception) {//Program exception capture/* * * * * * * * * * * SAVE Data code * * *//create Dump file HAN DLE hdumpfile = CreateFile (Qtime::currenttime (). toString ("hh when mm min ss seconds zzz.dmp"). UTF16 (), Generic_write, 0, NULL, Create_    Always, File_attribute_normal, NULL);        if (hdumpfile! = INVALID_HANDLE_VALUE) {//dump information minidump_exception_information dumpinfo;        Dumpinfo.exceptionpointers = pexception;        Dumpinfo.threadid = GetCurrentThreadID ();        Dumpinfo.clientpointers = TRUE; Write dump file Contents MiniDumpWriteDump (GetCurrentProcess (), GetCurrentProcessId (), Hdumpfile, Minidumpnormal, &dumpInfo    , NULL, NULL);    }//This pops up an error dialog box and exits the program exception_record* RECORD = pexception->exceptionrecord; QString Errcode (Qstring::number (record->exceptioncode,16)), Erradr (Qstring::number ((UINT) record->    exceptionaddress,16)), Errmod; Qmessagebox::critical (NULL, "program Crashes", "<font size=4><div><b> a sincere apology for the error that occurred </b><br/></div> "+ QString (" <div> error code:%1</div><div> error Address:%2</div></font> "). Arg (    Errcode). Arg (ERRADR), Qmessagebox::ok); return exception_execute_handler;}

When the error is troubled by the time, if God can directly tell the error in which line of code is how good! In fact WinDbg can do it.

In the Project Pro project file, add: Qmake_lflags_release =/incremental:no/debug

The purpose of this statement is that the release version will also generate a debug information file for the ". pdb" suffix. Before you use WinDbg to import a dump, specify the location of the source and PDB files to see which line of code the culprit is in the error report.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.