Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Execute destructors when pressing "stop"
Forum Updated to NodeBB v4.3 + New Features

Execute destructors when pressing "stop"

Scheduled Pinned Locked Moved Unsolved Qt Creator and other tools
7 Posts 3 Posters 1.2k 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.
  • M Offline
    M Offline
    Mark81
    wrote on last edited by
    #1

    In my application I catch the SIGTERM signal:

    #include <QCoreApplication>
    #include <QObject>
    
    #include "engine.h"
    #include "signalshandler.h"
    
    static int setup_unix_signal_handlers()
    {
        struct sigaction term;
    
        term.sa_handler = SignalsHandler::termSignalHandler;
        sigemptyset(&term.sa_mask);
        term.sa_flags = 0;
        term.sa_flags |= SA_RESTART;
    
        if (sigaction(SIGTERM, &term, 0)) return 1;
    
        return 0;
    }
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        SignalsHandler handler;
        setup_unix_signal_handlers();
    
        QObject::connect(&handler, &SignalsHandler::quit, &a, QCoreApplication::quit);
    
        //...
        return a.exec();
    }
    

    It works fine, i.e. if I try to kill my application from command line the destructors all executed and the application shuts down nicely.

    I want to achieve the same behavior when I press "stop" after launching the program from QtCreator.
    Here the behavior is not consistent. It seems that "some" destructors are executed, but not all of them. After a while the application "crashes":

    10:07:01: User requested stop. Shutting down...
    10:07:01: Remote process crashed.

    I placed breakpoints (or qDebug() calls) inside the destructors but as I said, not all of them are executed and they change from run to run.

    My wild guess is QtCreator after a while force the process to stop without waiting for the exit.

    Is there a way to do a correct shutdown of the application inside QtCreator?

    jsulmJ 1 Reply Last reply
    0
    • M Mark81

      In my application I catch the SIGTERM signal:

      #include <QCoreApplication>
      #include <QObject>
      
      #include "engine.h"
      #include "signalshandler.h"
      
      static int setup_unix_signal_handlers()
      {
          struct sigaction term;
      
          term.sa_handler = SignalsHandler::termSignalHandler;
          sigemptyset(&term.sa_mask);
          term.sa_flags = 0;
          term.sa_flags |= SA_RESTART;
      
          if (sigaction(SIGTERM, &term, 0)) return 1;
      
          return 0;
      }
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
          SignalsHandler handler;
          setup_unix_signal_handlers();
      
          QObject::connect(&handler, &SignalsHandler::quit, &a, QCoreApplication::quit);
      
          //...
          return a.exec();
      }
      

      It works fine, i.e. if I try to kill my application from command line the destructors all executed and the application shuts down nicely.

      I want to achieve the same behavior when I press "stop" after launching the program from QtCreator.
      Here the behavior is not consistent. It seems that "some" destructors are executed, but not all of them. After a while the application "crashes":

      10:07:01: User requested stop. Shutting down...
      10:07:01: Remote process crashed.

      I placed breakpoints (or qDebug() calls) inside the destructors but as I said, not all of them are executed and they change from run to run.

      My wild guess is QtCreator after a while force the process to stop without waiting for the exit.

      Is there a way to do a correct shutdown of the application inside QtCreator?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Mark81 said in Execute destructors when pressing "stop":

      I press "stop"

      What is "stop"?
      What exactly do you do when "stop" is pressed?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      0
      • jsulmJ jsulm

        @Mark81 said in Execute destructors when pressing "stop":

        I press "stop"

        What is "stop"?
        What exactly do you do when "stop" is pressed?

        M Offline
        M Offline
        Mark81
        wrote on last edited by Mark81
        #3

        @jsulm This is the "stop" button, as the tooltip says:

        Schermata da 2022-03-04 10-24-18.png

        What exactly do you do when "stop" is pressed?

        I do nothing :-)
        I'm expecting QtCreator sends a sigterm signal to my application. Because I'm catching it, and it's tied to the quit slot, I'm expecting the destructors are called.

        And it almost works, but (I guess) there is no time to execute all the destructor and my process crashes before completing the shutdown.

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          This button stops the application immediately. It's no button to properly shut down an application so no dtors are run.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          M 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            This button stops the application immediately. It's no button to properly shut down an application so no dtors are run.

            M Offline
            M Offline
            Mark81
            wrote on last edited by
            #5

            @Christian-Ehrlicher ok, is there a way to add a customized button in QtCreator? Or a way to send a char to stdin? I'm working on a headless remote device.

            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You can send a sigquit command to your app and react on this. You can send this signal with 'kill'.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              M 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                You can send a sigquit command to your app and react on this. You can send this signal with 'kill'.

                M Offline
                M Offline
                Mark81
                wrote on last edited by
                #7

                @Christian-Ehrlicher of course! This is what I'm doing right now:

                It works fine, i.e. if I try to kill my application from command line the destructors all executed and the application shuts down nicely.

                I was just looking for a convenient way within QtCreator.
                It seems they didn't think about this feature.

                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