Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Destructors not called when I terminate a console app with Ctrl-C
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
18 Posts 8 Posters 29.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    s.frings
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      danilocesar
      wrote on last edited by
      #2

      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).

      <a href="http://www.danilocesar.com">Danilo Cesar Lemes de Paula</a>
      Software Engineer

      1 Reply Last reply
      0
      • D Offline
        D Offline
        danilocesar
        wrote on last edited by
        #3

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

        <a href="http://www.danilocesar.com">Danilo Cesar Lemes de Paula</a>
        Software Engineer

        1 Reply Last reply
        0
        • D Offline
          D Offline
          danilocesar
          wrote on last edited by
          #4

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

          <a href="http://www.danilocesar.com">Danilo Cesar Lemes de Paula</a>
          Software Engineer

          1 Reply Last reply
          0
          • S Offline
            S Offline
            s.frings
            wrote on last edited by
            #5

            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).

            1 Reply Last reply
            0
            • D Offline
              D Offline
              danilocesar
              wrote on last edited by
              #6

              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.

              <a href="http://www.danilocesar.com">Danilo Cesar Lemes de Paula</a>
              Software Engineer

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lyuts
                wrote on last edited by
                #7

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

                I'm a rebel in the S.D.G.

                1 Reply Last reply
                0
                • ? This user is from outside of this forum
                  ? This user is from outside of this forum
                  Guest
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kalle
                    wrote on last edited by
                    #9

                    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).

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      tobias.hunger
                      wrote on last edited by
                      #10

                      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>").

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        danilocesar
                        wrote on last edited by
                        #11

                        [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.

                        "http://en.wikipedia.org/wiki/SIGINT_(POSIX)":http://en.wikipedia.org/wiki/SIGINT_(POSIX)

                        "http://en.wikipedia.org/wiki/SIGTERM":http://en.wikipedia.org/wiki/SIGTERM

                        [edit: fixed link / chetankjain]

                        <a href="http://www.danilocesar.com">Danilo Cesar Lemes de Paula</a>
                        Software Engineer

                        1 Reply Last reply
                        0
                        • T Offline
                          T Offline
                          tobias.hunger
                          wrote on last edited by
                          #12

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

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            s.frings
                            wrote on last edited by
                            #13

                            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.

                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              danilocesar
                              wrote on last edited by
                              #14

                              @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...

                              <a href="http://www.danilocesar.com">Danilo Cesar Lemes de Paula</a>
                              Software Engineer

                              1 Reply Last reply
                              0
                              • T Offline
                                T Offline
                                tobias.hunger
                                wrote on last edited by
                                #15

                                @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.

                                1 Reply Last reply
                                0
                                • A Offline
                                  A Offline
                                  aekam
                                  wrote on last edited by
                                  #16

                                  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.??]

                                  If you take care of inches, you won't have to worry about miles... :)

                                  1 Reply Last reply
                                  0
                                  • X Offline
                                    X Offline
                                    xiangzhai
                                    wrote on last edited by
                                    #17

                                    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);
                                    ...
                                    }

                                    @

                                    1 Reply Last reply
                                    0
                                    • D Offline
                                      D Offline
                                      DungeonLords
                                      wrote on last edited by
                                      #18

                                      This example explain how to use SIGINT (terminate app using Ctrl+C).

                                      1 Reply Last reply
                                      0

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved