⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

Destructors not called when I terminate a console app with Ctrl-C

  • Hello,
    on windows, when I enter Ctrl-C while any console app is running, QT-Creator shows a negative exit code, for example -1073741510.

    On Linux, I also see a negative return code plus a remark that the application finished unexpectedly. If I set the GUI language to german it says that the application crashed (in german words).

    Is that normal or a technical problem? Example source to reproduce:

    @int main(int argc, char *argv[]) {
    QCoreApplication app(argc, argv);
    return app.exec();

    I think, it's fairly impossible to make a mistake in that simple application.

  • When you close your app using CTRL+C, your app.exec() method doesn't return. Your app is killed by an unix signal called "SIGINT":http://en.wikipedia.org/wiki/SIGINT_(POSIX).

  • If you're looking for last second clean-up, you should use "QCoreApplication::aboutToQuit":http://doc.trolltech.com/4.4/qcoreapplication.html#aboutToQuit signal

  • Ah, I'm talking about Linux... I'm not sure how windows implements those stuffs.

  • Thanks for yout hint. I tried it but it does not work under windows, unfortunately. I do not see my debug message from the slot that I assigned to aboutToQuit() and I still get random exit codes when I pres Ctrl-C.

    Did I understand you correctly, that it is normal that app.exec() does not return, so the program is simply killed without any cleanup (except that the OS closes file handles ans some other stuff).

  • Sorry if I couldn't made myself clear, but aboutToQuit will not work for CTRL+C.

    I've just tried to say that aboutToQuit is safer for cleanup's, because I thought you're trying to do that on destructos.

    About your last comment: Afaik yes... However you can watch for "unix signals":http://doc.qt.nokia.com/4.6/unix-signals.html. There is an interesting thread about it on "stackoverflow":http://stackoverflow.com/questions/2300401/qapplication-how-to-shutdown-gracefully-on-ctrl-c.

  • I think the thread title doesn't correspond thread's topic. This is confusing.

  • chk if "this faq":http://developer.qt.nokia.com/faq/answer/how_can_i_catch_altf4_in_my_qt_application helps, and if you can take a similar approach to handle CTRL+C

  • aboutToQuit() won't help, as was previously posted, and neither will qAddPostRoutine(). Look up the man page for the signal() system function to catch SIGKILL (which is what Ctrl-C sends).

  • kalle: Ctrl-C does send SIGTERM, not SIGKILL.

    You can not catch SIGKILL, that one will always kill the application (and is what if send via the command "kill -9 <PID>").

  • [quote author="Tobias Hunger" date="1287045094"]kalle: Ctrl-C does send SIGTERM, not SIGKILL.

    You can not catch SIGKILL, that one will always kill the application (and is what if send via the command "kill -9 <PID>").[/quote]

    Afaik no.

    SIGTERM is the default signal sent by kill (without -9)
    SIGINT is the signal sent by terminal when you press ctrl-c.



    [edit: fixed link / chetankjain]

  • danilocesar: You are right. I am getting old and start mixing up things:-(

  • I am already familiar with signal catching under Linux and Im sure that this would be the solution for my question if I would only deveop for Linux. However, Im currently more interested in Windows.

    Anyway, the abnormal program termination is not really a problem to me. I just wondered if the error message in QT Creator and the random exit code indicate a problem or if that is normal.

    My understanding is now that it is normal to see a abnormal termination warning and a random exit code when I press Ctrl-C because the QCoreApplication.exec() method does not return, so the program does not return a defined exit code.

  • @Tobias: What do you think about this:

    QCoreApplication could automatically catch SIGTERM and SIGINT (and relatives for windows/mac/symbian/whatever) and return pre-defined values for the exec() method.

    I don't know if it's portable, but it can be useful...

  • @danilocesar: Qt applications already handle signals: The nice thing is that the operating system enforces this, so there is no code required in Qt (always the best way to implement anything;-). If the defaults do not work for you, then you can override it. So where is the problem?

    What needs to happen when a signal is received is usually platform dependent, so there is little to win by proving an abstraction in Qt.

  • i have written a console application.
    now suppose if i send SIGINT to the application, will it get terminate immediately.??
    or it will complete all the pending stuff and then return.??

    [EDITED: aboutToQuit signal may help, once the SIGINT is received and event loop returns, this signal will be emitted and clean up can be done in the slot connected to it... right.??]

  • I prefer to use signal management, such as

    #include <signal.h>

    static void m_cleanup(int sig)
    qDebug() << "Bye :)";
    // your destructor stuff
    if (m_obj) delete m_obj; m_obj = NULL;
    if (sig == SIGINT) qApp->quit();

    int main(int argc, char *argv[])
    signal(SIGINT, m_cleanup);


Log in to reply