mardi 4 août 2015

C++ Crash/exception handler in Qt application with thread support on Windows

I want to make a crash/exception handler for my Qt application. I have the handler working already (not included in the code below). The problem is on Windows that it only works if the crash occures in the same thread where signal() and std::set_terminate() is called.

On Linux it seems to work for all threads by default.

Is there a way to make it work for all application threads on Windows?

#include "mainwindow.h"
#include <QApplication>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <exception>

void seg_handler(int sig)
{
    // Crash/exception handling code
    // ...
    exit(1);
}

void std_handler( void ) {
     seg_handler(1);
}

int main(int argc, char *argv[]) {

    signal(SIGSEGV, seg_handler);
    std::set_terminate( std_handler );

    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

Aucun commentaire:

Enregistrer un commentaire