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. segfault in QSqlDatabase::database when used in the destructor of a static variable when closing the app
Forum Updated to NodeBB v4.3 + New Features

segfault in QSqlDatabase::database when used in the destructor of a static variable when closing the app

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 4 Posters 927 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.
  • Christian EhrlicherC Christian Ehrlicher

    As mentioned every time when someone is using a QSqlDatabase instance as member - don't do it. It's properly documented how to use QSqlDatabase:

    Warning: It is highly recommended that you do not keep a copy of the QSqlDatabase around as a member of a class, as this will prevent the instance from being correctly cleaned up on shutdown. If you need to access an existing QSqlDatabase, it should be accessed with database(). If you chose to have a QSqlDatabase member variable, this needs to be deleted before the QCoreApplication instance is deleted, otherwise it may lead to undefined behavior.

    mbruelM Offline
    mbruelM Offline
    mbruel
    wrote on last edited by mbruel
    #3

    @Christian-Ehrlicher
    I don't use a QSqlDatabase instance as a member... I'm using QSqlDatabase::database static method and it is this one that crashes...
    but thanks, I guess I got my answer with this needs to be deleted before the QCoreApplication instance is deleted
    I suppose QSqlDatabase::database can't be used after that the QCoreApplication has been deleted too. this could be added to the documentation of the method...
    That is my case I guess calling it in my static singleton destructor.

    JonBJ Christian EhrlicherC KroMignonK 3 Replies Last reply
    0
    • mbruelM mbruel

      @Christian-Ehrlicher
      I don't use a QSqlDatabase instance as a member... I'm using QSqlDatabase::database static method and it is this one that crashes...
      but thanks, I guess I got my answer with this needs to be deleted before the QCoreApplication instance is deleted
      I suppose QSqlDatabase::database can't be used after that the QCoreApplication has been deleted too. this could be added to the documentation of the method...
      That is my case I guess calling it in my static singleton destructor.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #4

      @mbruel
      I guess it's taken you shouldn't try to use most things Qt after Q...Application is destroyed. Like you could try to still use a QWidget, but I wouldn't recommend it....

      mbruelM 1 Reply Last reply
      0
      • mbruelM mbruel

        @Christian-Ehrlicher
        I don't use a QSqlDatabase instance as a member... I'm using QSqlDatabase::database static method and it is this one that crashes...
        but thanks, I guess I got my answer with this needs to be deleted before the QCoreApplication instance is deleted
        I suppose QSqlDatabase::database can't be used after that the QCoreApplication has been deleted too. this could be added to the documentation of the method...
        That is my case I guess calling it in my static singleton destructor.

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #5

        @mbruel said in segfault in QSqlDatabase::database when used in the destructor of a static variable when closing the app:

        This could be added to the documentation of the method...

        Then you have to add this to >90% of all Qt functions...

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

        1 Reply Last reply
        1
        • mbruelM mbruel

          @Christian-Ehrlicher
          I don't use a QSqlDatabase instance as a member... I'm using QSqlDatabase::database static method and it is this one that crashes...
          but thanks, I guess I got my answer with this needs to be deleted before the QCoreApplication instance is deleted
          I suppose QSqlDatabase::database can't be used after that the QCoreApplication has been deleted too. this could be added to the documentation of the method...
          That is my case I guess calling it in my static singleton destructor.

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by KroMignon
          #6

          @mbruel said in segfault in QSqlDatabase::database when used in the destructor of a static variable when closing the app:

          I suppose QSqlDatabase::database can't be used after that the QCoreApplication has been deleted too. this could be added to the documentation of the method...

          AFAIK, in documentation, you will found that QCoreApplication/QGuiApplication/QApplication must be the first object to create before doing anything with Qt classes.
          It holds global information which are used by most of Qt classes and also the main event loop.

          So it is implicit, to me, that it should be the last which should be destroyed.

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          JonBJ 1 Reply Last reply
          3
          • KroMignonK KroMignon

            @mbruel said in segfault in QSqlDatabase::database when used in the destructor of a static variable when closing the app:

            I suppose QSqlDatabase::database can't be used after that the QCoreApplication has been deleted too. this could be added to the documentation of the method...

            AFAIK, in documentation, you will found that QCoreApplication/QGuiApplication/QApplication must be the first object to create before doing anything with Qt classes.
            It holds global information which are used by most of Qt classes and also the main event loop.

            So it is implicit, to me, that it should be the last which should be destroyed.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #7

            @KroMignon
            Purely OOI (untested), doesn't this apply to something like QObject classes? Can you use, say, a QVector outside of Q...Application, or is even that not allowed?

            KroMignonK 1 Reply Last reply
            0
            • JonBJ JonB

              @KroMignon
              Purely OOI (untested), doesn't this apply to something like QObject classes? Can you use, say, a QVector outside of Q...Application, or is even that not allowed?

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by KroMignon
              #8

              @JonB said in segfault in QSqlDatabase::database when used in the destructor of a static variable when closing the app:

              Purely OOI (untested), doesn't this apply to something like QObject classes? Can you use, say, a QVector outside of Q...Application, or is even that not allowed?

              Especially QObject classes!
              A good coding practice with Qt is to first create QCoreApplication/QGuiApplication/QApplication at beginning of main(), in fact you don't really have to destroy it.

              int main(int argc, char *argv[])
              {
                  QCoreApplication app(argc, argv);
                  QCoreApplication::setApplicationName("...");
                  QCoreApplication::setOrganizationName("...");
                  QCoreApplication::setApplicationVersion("1.0.0");
              
                  // continue initialization
                  ...
              
                  // start main loop and wait application end
                  return app.exec();
              }
              

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              1 Reply Last reply
              0
              • JonBJ JonB

                @mbruel
                I guess it's taken you shouldn't try to use most things Qt after Q...Application is destroyed. Like you could try to still use a QWidget, but I wouldn't recommend it....

                mbruelM Offline
                mbruelM Offline
                mbruel
                wrote on last edited by
                #9

                @JonB said in segfault in QSqlDatabase::database when used in the destructor of a static variable when closing the app:

                I guess it's taken you shouldn't try to use most things Qt after Q...Application is destroyed. Like you could try to still use a QWidget, but I wouldn't recommend it....

                well for QObject that uses the event loop definitely but QSqlDatabase doesn't seem to be a QObject...
                and in general I obviously don't do this kind of things but using a static singleton as the Main app that would do some work on Database on its destructor could really happen... I guess in that case it should own the QApplication and delete it as the latest. On the project I'm working on it is not the case, the QApplication is defined in the main.cpp.

                @JonB said in segfault in QSqlDatabase::database when used in the destructor of a static variable when closing the app:

                Purely OOI (untested), doesn't this apply to something like QObject classes? Can you use, say, a QVector outside of Q...Application, or is even that not allowed?

                I believe you could use QVector and other Qt objects without a QApplication. I didn't test but I don't see why not... as long as they don't use the event loop...

                @KroMignon said in segfault in QSqlDatabase::database when used in the destructor of a static variable when closing the app:

                A good coding practice with Qt is to first create QCoreApplication/QGuiApplication/QApplication at beginning of main(), in fact you don't really have to destroy it.

                well in general yes... for small programs. but it is quite common to rather embed the QCoreApplication/QGuiApplication/QApplication in the Object representing your application, especially if you parse the command line and can either launch a GUI or stay PURE command line (only QCoreApplication)
                you can find an example of such main.cpp here where my app derives from CmdOrGuiApp.

                KroMignonK 1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  Don't use global static singletons.

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

                  mbruelM 1 Reply Last reply
                  1
                  • mbruelM mbruel

                    @JonB said in segfault in QSqlDatabase::database when used in the destructor of a static variable when closing the app:

                    I guess it's taken you shouldn't try to use most things Qt after Q...Application is destroyed. Like you could try to still use a QWidget, but I wouldn't recommend it....

                    well for QObject that uses the event loop definitely but QSqlDatabase doesn't seem to be a QObject...
                    and in general I obviously don't do this kind of things but using a static singleton as the Main app that would do some work on Database on its destructor could really happen... I guess in that case it should own the QApplication and delete it as the latest. On the project I'm working on it is not the case, the QApplication is defined in the main.cpp.

                    @JonB said in segfault in QSqlDatabase::database when used in the destructor of a static variable when closing the app:

                    Purely OOI (untested), doesn't this apply to something like QObject classes? Can you use, say, a QVector outside of Q...Application, or is even that not allowed?

                    I believe you could use QVector and other Qt objects without a QApplication. I didn't test but I don't see why not... as long as they don't use the event loop...

                    @KroMignon said in segfault in QSqlDatabase::database when used in the destructor of a static variable when closing the app:

                    A good coding practice with Qt is to first create QCoreApplication/QGuiApplication/QApplication at beginning of main(), in fact you don't really have to destroy it.

                    well in general yes... for small programs. but it is quite common to rather embed the QCoreApplication/QGuiApplication/QApplication in the Object representing your application, especially if you parse the command line and can either launch a GUI or stay PURE command line (only QCoreApplication)
                    you can find an example of such main.cpp here where my app derives from CmdOrGuiApp.

                    KroMignonK Offline
                    KroMignonK Offline
                    KroMignon
                    wrote on last edited by
                    #11

                    @mbruel said in segfault in QSqlDatabase::database when used in the destructor of a static variable when closing the app:

                    well in general yes... for small programs. but it is quite common to rather embed the QCoreApplication/QGuiApplication/QApplication in the Object representing your application, especially if you parse the command line and can either launch a GUI or stay PURE command line (only QCoreApplication)
                    you can find an example of such main.cpp here where my app derives from CmdOrGuiApp.

                    No this is not common!
                    You never have to use QObject before creating QCoreApplication/QGuiApplication/QApplication.
                    Of course, sometimes you want to parse command line before creating one of those, but when doing this, you do not use QObject based classes but only C++ standard functions like this example taken from Qt documentation:

                    QCoreApplication* createApplication(int &argc, char *argv[])
                    {
                        for (int i = 1; i < argc; ++i) {
                            if (!qstrcmp(argv[i], "-no-gui"))
                                return new QCoreApplication(argc, argv);
                        }
                        return new QApplication(argc, argv);
                    }
                    
                    int main(int argc, char* argv[])
                    {
                        QScopedPointer<QCoreApplication> app(createApplication(argc, argv));
                    
                        if (qobject_cast<QApplication *>(app.data())) {
                           // start GUI version...
                        } else {
                           // start non-GUI version...
                        }
                    
                        return app->exec();
                    }
                    

                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                    1 Reply Last reply
                    3
                    • Christian EhrlicherC Christian Ehrlicher

                      Don't use global static singletons.

                      mbruelM Offline
                      mbruelM Offline
                      mbruel
                      wrote on last edited by
                      #12

                      @Christian-Ehrlicher said in segfault in QSqlDatabase::database when used in the destructor of a static variable when closing the app:

                      Don't use global static singletons.

                      A Singleton class like this is quite useful and there is no issue with it as you can connect QCoreApplication::aboutToQuit to the deletion of its instance if needed (so before the destruction of the QCoreApplication).
                      It's a matter of taste, personally I find it really convenient to be able to use a singleton using MyObject::instance() rather than having to provide an handle to it (either in function calls or in the constructor and storing it as a member)

                      @KroMignon said in segfault in QSqlDatabase::database when used in the destructor of a static variable when closing the app:

                      You never have to use QObject before creating QCoreApplication/QGuiApplication/QApplication.
                      Of course, sometimes you want to parse command line before creating one of those, but when doing this, you do not use QObject based classes but only C++ standard functions like this example taken from Qt documentation

                      well of course you don't have to use a QObject, but who says you shouldn't if it is convenient for you?...
                      If you look QCoreApplication documentation, its main goal is to provide an event loop for your application (main thread).
                      There is no issue creating QObject before or without it as long as you don't uses events, is there?
                      I've done few softwares where I create my main QObject that will instantiate the QCoreApplication and it's working just fine. This main object is then able to use the event queue...
                      I don't understand why you would say we shouldn't do that, what is the reason?
                      Some people even inherit from QCoreApplication but for what I read few years ago, it was more advised to initiate it (and own it) than subclassing (this allowing to be able to choose which instance you need depending on whatever condition)

                      KroMignonK 1 Reply Last reply
                      0
                      • mbruelM mbruel

                        @Christian-Ehrlicher said in segfault in QSqlDatabase::database when used in the destructor of a static variable when closing the app:

                        Don't use global static singletons.

                        A Singleton class like this is quite useful and there is no issue with it as you can connect QCoreApplication::aboutToQuit to the deletion of its instance if needed (so before the destruction of the QCoreApplication).
                        It's a matter of taste, personally I find it really convenient to be able to use a singleton using MyObject::instance() rather than having to provide an handle to it (either in function calls or in the constructor and storing it as a member)

                        @KroMignon said in segfault in QSqlDatabase::database when used in the destructor of a static variable when closing the app:

                        You never have to use QObject before creating QCoreApplication/QGuiApplication/QApplication.
                        Of course, sometimes you want to parse command line before creating one of those, but when doing this, you do not use QObject based classes but only C++ standard functions like this example taken from Qt documentation

                        well of course you don't have to use a QObject, but who says you shouldn't if it is convenient for you?...
                        If you look QCoreApplication documentation, its main goal is to provide an event loop for your application (main thread).
                        There is no issue creating QObject before or without it as long as you don't uses events, is there?
                        I've done few softwares where I create my main QObject that will instantiate the QCoreApplication and it's working just fine. This main object is then able to use the event queue...
                        I don't understand why you would say we shouldn't do that, what is the reason?
                        Some people even inherit from QCoreApplication but for what I read few years ago, it was more advised to initiate it (and own it) than subclassing (this allowing to be able to choose which instance you need depending on whatever condition)

                        KroMignonK Offline
                        KroMignonK Offline
                        KroMignon
                        wrote on last edited by KroMignon
                        #13

                        @mbruel said in segfault in QSqlDatabase::database when used in the destructor of a static variable when closing the app:

                        I don't understand why you would say we shouldn't do that, what is the reason?

                        I am only sharing my experience with you, if you think it is not relevant to you, never mind. This is not a problem for me.

                        And if you look to QCoreApplication documentation, you will find this:

                        In general, we recommend that you create a QCoreApplication, QGuiApplication or a QApplication object in your main() function as early as possible. exec() will not return until the event loop exits; e.g., when quit() is called.

                        The reason is to avoid having to find strange bugs in your application, like qDebug() not working (which I had to find some years ago).

                        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                        mbruelM 1 Reply Last reply
                        0
                        • KroMignonK KroMignon

                          @mbruel said in segfault in QSqlDatabase::database when used in the destructor of a static variable when closing the app:

                          I don't understand why you would say we shouldn't do that, what is the reason?

                          I am only sharing my experience with you, if you think it is not relevant to you, never mind. This is not a problem for me.

                          And if you look to QCoreApplication documentation, you will find this:

                          In general, we recommend that you create a QCoreApplication, QGuiApplication or a QApplication object in your main() function as early as possible. exec() will not return until the event loop exits; e.g., when quit() is called.

                          The reason is to avoid having to find strange bugs in your application, like qDebug() not working (which I had to find some years ago).

                          mbruelM Offline
                          mbruelM Offline
                          mbruel
                          wrote on last edited by
                          #14

                          @KroMignon well you were quite affirmative in your replies...
                          I care of other people experiences and opinions but we're also here to understand what's possible, what's advised, what's not advised and most of all what's not possible preferably with a concrete explanation

                          KroMignonK 1 Reply Last reply
                          0
                          • mbruelM mbruel

                            @KroMignon well you were quite affirmative in your replies...
                            I care of other people experiences and opinions but we're also here to understand what's possible, what's advised, what's not advised and most of all what's not possible preferably with a concrete explanation

                            KroMignonK Offline
                            KroMignonK Offline
                            KroMignon
                            wrote on last edited by KroMignon
                            #15

                            @mbruel said in segfault in QSqlDatabase::database when used in the destructor of a static variable when closing the app:

                            well you were quite affirmative in your replies...
                            I care of other people experiences and opinions but we're also here to understand what's possible, what's advised, what's not advised and most of all what's not possible preferably with a concrete explanation

                            Sorry about that, I am a french guy living in Germany and writing in english... Sometimes there are too many languages in my head and I don't find the right words.
                            I will try to take more care about my writing to be less directive.

                            But this was a really hard bug to find, only because I create QCoreApplication too late in my main()

                            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                            mbruelM 1 Reply Last reply
                            0
                            • KroMignonK KroMignon

                              @mbruel said in segfault in QSqlDatabase::database when used in the destructor of a static variable when closing the app:

                              well you were quite affirmative in your replies...
                              I care of other people experiences and opinions but we're also here to understand what's possible, what's advised, what's not advised and most of all what's not possible preferably with a concrete explanation

                              Sorry about that, I am a french guy living in Germany and writing in english... Sometimes there are too many languages in my head and I don't find the right words.
                              I will try to take more care about my writing to be less directive.

                              But this was a really hard bug to find, only because I create QCoreApplication too late in my main()

                              mbruelM Offline
                              mbruelM Offline
                              mbruel
                              wrote on last edited by
                              #16

                              @KroMignon
                              pas de problèmes ;)
                              yeah it's defo something unusual that can bring unexpected issues and/or crashes...

                              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