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. Segmentation fault on SIGINT interception when trying to exit the QCoreApplication

Segmentation fault on SIGINT interception when trying to exit the QCoreApplication

Scheduled Pinned Locked Moved Solved General and Desktop
exitsignalsigintqcoreapplicatio
8 Posts 3 Posters 5.4k Views
  • 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
    mbruel
    wrote on 27 Dec 2015, 23:38 last edited by
    #1

    Hi,
    I want to use a cleanup function when my QCoreApplication is destroyed (intercepting a SIGINT).
    I've tried two methods:
    First to connect the QCoreApplication::aboutToQuit to a slot that would do the job but I don't end up in the slot when the application is closed by a SIGINT or even if close manually the app (launched via QT Creator)...

    So my second option would be to use signal interceptions. Here is what I do:

    #include <csignal>
    
    Proxy *theProxy   = NULL;
    QCoreApplication *app = NULL;
    
    void handleShutDown(int signal){
        Proxy::shutDown();
        std::cout << "Try to exit the event loop\n";
        app->exit(0);
    }
    
    int main(int argc, char *argv[])
    {
        app = new QCoreApplication(argc, argv);
    
        signal(SIGINT,  &handleShutDown);// shut down on ctrl-c
        signal(SIGTERM, &handleShutDown);// shut down on killall
    
    
        theProxy = Proxy::getInstance();
    //    QObject::connect(app, &QCoreApplication::aboutToQuit, theProxy, &Proxy::deleteInstance);
    
        if (!theProxy->start()){
            std::cout << "Error starting Proxy...\n";
            return 1;
        }
    
        app->exec(); // Run the event loop
    
        std::cout << "Event loop ended...";
        return 0;
    }
    

    So here is what I get when I stop my app with a SIGINT:

    ^CTry to exit the event loop
    Segmentation fault
    

    I can see that the cleanup funtion is well executed but then there is a segmentation fault when I try to exit the QCoreApplication.
    I've no clue how I could debug this...
    Any idea what I'm doing wrong or how I could get traces to understand more the issue?

    Thanks.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 28 Dec 2015, 00:12 last edited by kshegunov
      #2

      Hello,
      Firstly, do not create the QCoreApplication instance in the heap, you gain only headaches. Secondly, if you really need a global static variable (which I believe you don't) you should use Q_GLOBAL_STATIC. You would want to intercept the signals only if you have some very specific setup where you need to cleanup some global resources that don't belong to your application (i.e. a shutdown routine for a driver). Global variables are like premature optimization, they're troublesome and in most cases purely unnecessary, don't use them unless you're forced to.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 28 Dec 2015, 00:23 last edited by
        #3

        Hi,

        To add to @kshegunov, you can access the instance of QCoreApplication through qApp.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mbruel
          wrote on 28 Dec 2015, 02:52 last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • M Offline
            M Offline
            mbruel
            wrote on 28 Dec 2015, 03:08 last edited by mbruel
            #5

            Ok, I've let the QCoreApplication on the stack and don't use anymore a global variable for my Proxy. I don't really need to as I'm using the singleton pattern. Both constructor and destructor are private.
            I'm using a static method to get/create the instance and another one to delete it.
            Here is the new code.

            #include <csignal>
            
            void handleShutDown(int signal){
                std::cout << "Try to shutdown the proxy\n";
                Proxy::shutDown();
                std::cout << "Try to exit the event loop\n";
                qApp->exit(0);
            }
            
            int main(int argc, char *argv[])
            {
                QCoreApplication app(argc, argv);
            
                signal(SIGINT,  &handleShutDown);// shut down on ctrl-c
                signal(SIGTERM, &handleShutDown);// shut down on killall
            
                Proxy *theProxy = Proxy::getInstance();
            //    QObject::connect(app, &QCoreApplication::aboutToQuit, theProxy, &Proxy::deleteInstance);
            
                if (!theProxy->start()){
                    std::cout << "Error starting Proxy...\n";
                    return 1;
                }
            
                app.exec(); // Run the event loop
            
                std::cout << "Event loop ended...";
                return 0;
            }
            

            I'm still having the same segmentation fault. Any idea why? The object I'm deleting inherit from QTcpServer and is just listening.
            I'm having the crash without having done anything.
            As I said, the object is destructed and the crash seems to happen after on the exit call to stop the event loop.

            Server started, listening on port: 11111
            ^CTry to shutdown the proxy
            Try to exit the event loop
            Segmentation fault
            

            PS: I need to have a cleanup function. In most cases, my program will be stopped by an interruption and I want to make sure it releases the resources properly and take the time to write final logs before shutting down.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mbruel
              wrote on 28 Dec 2015, 16:07 last edited by
              #6

              Alright the handling of the shutdown interruption is working fine.
              I've managed to find what was causing the segmentation fault. It is the construction/deletion of my SQL class that do a QSqlDatabase::addDatabase...
              I'm going to try to understand what could be the problem and open another post if I don't manage to get what is wrong.

              K 1 Reply Last reply 28 Dec 2015, 16:46
              0
              • M mbruel
                28 Dec 2015, 16:07

                Alright the handling of the shutdown interruption is working fine.
                I've managed to find what was causing the segmentation fault. It is the construction/deletion of my SQL class that do a QSqlDatabase::addDatabase...
                I'm going to try to understand what could be the problem and open another post if I don't manage to get what is wrong.

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 28 Dec 2015, 16:46 last edited by
                #7

                @mbruel
                Is your SQL class by any chance a singleton as well? If you answer yes, then this is one of the reasons you should not use global variables (a singleton is pretty much the same as a global variable).

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 28 Dec 2015, 22:32 last edited by
                  #8

                  Also, what backtrace to you get from the debugger ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  1/8

                  27 Dec 2015, 23:38

                  • Login

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