Clean before closing QCoreApplication

I have a console QCoreApplicationthat has timers and performs socket communication, and also uses locked mutex.

When I close the application manually, it gives an error saying that some mutex is locked and it is disabled. Is there a way I can do in a console application when the user closes it?

+5
source share
3 answers

Purge must be handled by destructors and child and parent relationships.

Make your main object (primary) a child of QApplication so that it is destroyed by all its children before QApplication.

, ? eventloop, QThread::quit() eventloop QThread::wait()

void QApplication::qAddPostRoutine ( QtCleanUpFunction ptr ) .

QtMsgHandler qInstallMsgHandler ( QtMsgHandler h ) , . , , .

void debugMessageHandler( QtMsgType type, const char *msg ){
    if(QString(msg).contains( "The message you can see in the console" )){
        int breakPointOnThisLine(0);    
    }

    switch ( type ) {
        case QtDebugMsg:
            fprintf( stderr, "Debug: %s\n", msg );
            break;
        case QtWarningMsg:
            fprintf( stderr, "Warning: %s\n", msg );
            break;
        case QtFatalMsg:
            fprintf( stderr, "Fatal: %s\n", msg );
            abort();
    }
}

-, QCoreApplication::exit() .

#include <csignal>
#include <QtCore/QCoreApplication>
using namespace std;

struct CleanExit{
    CleanExit() {
        signal(SIGINT, &CleanExit::exitQt);
        signal(SIGTERM, &CleanExit::exitQt);
        signal(SIGBREAK, &CleanExit::exitQt) ;
    }

    static void exitQt(int sig) {
        QCoreApplication::exit(0);
    }
};


int main(int argc, char *argv[])
{
    CleanExit cleanExit;
    QCoreApplication a(argc, argv);
    return a.exec();
}
+15

, ( Win7 VS2010), "" ( x ), STATUS_CONTROL_C_EXIT . .

" " (0x980) -1073741510 (0xC000013A).

'QThread' (0x2388) -1073741510 (0xc000013a).

, QCoreApplication::aboutToQuit().

winnt.h ntstatus.h. , STATUS_CONTROL_C_EXIT. , .

+5

you can connect to QCoreApplication :: aboutToQuit and perform the necessary cleanup.

+1
source

All Articles