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. QSqlDatabase open() crashes when db cannot be accessed ?
QtWS25 Last Chance

QSqlDatabase open() crashes when db cannot be accessed ?

Scheduled Pinned Locked Moved Solved General and Desktop
qsqldatabaseqpsql
27 Posts 5 Posters 4.7k 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.
  • R Offline
    R Offline
    R-P-H
    wrote on 16 Jul 2020, 18:59 last edited by
    #1

    Hi, my application segfaults when trying to open a remote PostgreSQL database that is not accessible. When the database is accessible it works fine.

    #include <QSqlDatabase>
    QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    db.setHostName("my_host");
    db.setPort(5432);
    db.setDatabaseName("my_db");
    db.setUserName("my_user");
    db.setPassword("my_password");
    bool ok = db.open(); //-> crashes here after hanging for about 10 to 20 seconds
    //...error checking if ok
    

    I did see other posts about similar issues with mysql but they are nearly 5 years old.

    J 1 Reply Last reply 16 Jul 2020, 19:24
    0
    • R Offline
      R Offline
      R-P-H
      wrote on 17 Jul 2020, 21:31 last edited by
      #20

      So through much debugging I finally found the issue. Just 1 line of code:

      QCoreApplication::processEvents();
      

      I have a thread that runs at application startup and emits an image to a label for displaying. The above line of code was at the end of this loop. Removing the line of code doesn't appear to cause any other issues and solves the segfault when trying to open the database.

      Why this causes the segfault in the first place is still bizarre to me...

      1 Reply Last reply
      0
      • R R-P-H
        16 Jul 2020, 18:59

        Hi, my application segfaults when trying to open a remote PostgreSQL database that is not accessible. When the database is accessible it works fine.

        #include <QSqlDatabase>
        QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
        db.setHostName("my_host");
        db.setPort(5432);
        db.setDatabaseName("my_db");
        db.setUserName("my_user");
        db.setPassword("my_password");
        bool ok = db.open(); //-> crashes here after hanging for about 10 to 20 seconds
        //...error checking if ok
        

        I did see other posts about similar issues with mysql but they are nearly 5 years old.

        J Offline
        J Offline
        JonB
        wrote on 16 Jul 2020, 19:24 last edited by
        #2

        @R-P-H
        Would it help others to state the Qt version, operating system etc.?
        Were you able to say whether it happens on any other versions?

        R 1 Reply Last reply 16 Jul 2020, 19:54
        0
        • J JonB
          16 Jul 2020, 19:24

          @R-P-H
          Would it help others to state the Qt version, operating system etc.?
          Were you able to say whether it happens on any other versions?

          R Offline
          R Offline
          R-P-H
          wrote on 16 Jul 2020, 19:54 last edited by R-P-H
          #3

          @JonB said in QSqlDatabase open() crashes when db cannot be accessed ?:

          @R-P-H
          Would it help others to state the Qt version, operating system etc.?
          Were you able to say whether it happens on any other versions?

          @JonB Qt 5.14.0. MinGW 64-bit. Windows 10. No I have not tested with any other versions yet.

          C 1 Reply Last reply 16 Jul 2020, 20:02
          0
          • R R-P-H
            16 Jul 2020, 19:54

            @JonB said in QSqlDatabase open() crashes when db cannot be accessed ?:

            @R-P-H
            Would it help others to state the Qt version, operating system etc.?
            Were you able to say whether it happens on any other versions?

            @JonB Qt 5.14.0. MinGW 64-bit. Windows 10. No I have not tested with any other versions yet.

            C Offline
            C Offline
            Cobra91151
            wrote on 16 Jul 2020, 20:02 last edited by Cobra91151
            #4

            @R-P-H

            Hello!

            I am curious, while it hanging for about 10 to 20 seconds, can you check this code: qDebug() << "isOpenError: " << db.isOpenError() << "\n" << db.lastError().nativeErrorCode() << "\n" << db.lastError().text();. What will it print?

            R 1 Reply Last reply 16 Jul 2020, 20:13
            0
            • C Cobra91151
              16 Jul 2020, 20:02

              @R-P-H

              Hello!

              I am curious, while it hanging for about 10 to 20 seconds, can you check this code: qDebug() << "isOpenError: " << db.isOpenError() << "\n" << db.lastError().nativeErrorCode() << "\n" << db.lastError().text();. What will it print?

              R Offline
              R Offline
              R-P-H
              wrote on 16 Jul 2020, 20:13 last edited by
              #5

              @Cobra91151 said in QSqlDatabase open() crashes when db cannot be accessed ?:

              @R-P-H

              Hello!

              I am curious, while it hanging for about 10 to 20 seconds, can you check this code: qDebug() << "isOpenError: " << db.isOpenError() << "\n" << db.lastError().nativeErrorCode() << "\n" << db.lastError().text();. What will it print?

              Hi !

              isOpenError:  true 
               "" 
               "could not connect to server: Connection timed out (0x0000274C/10060)\n\tIs the server running on host \"REDACTED\" and accepting\n\tTCP/IP connections on port 5432?\nQPSQL: Unable to connect"
              22:09:22: The program has unexpectedly finished.
              
              1 Reply Last reply
              0
              • C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 16 Jul 2020, 20:27 last edited by
                #6

                So did you actually check if the server is running there? A simple telnet from the host to the server at port 5432 should show you if it's reachable.

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

                S R 2 Replies Last reply 16 Jul 2020, 20:28
                0
                • C Christian Ehrlicher
                  16 Jul 2020, 20:27

                  So did you actually check if the server is running there? A simple telnet from the host to the server at port 5432 should show you if it's reachable.

                  S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 16 Jul 2020, 20:28 last edited by
                  #7

                  @Christian-Ehrlicher said in QSqlDatabase open() crashes when db cannot be accessed ?:

                  So did you actually check if the server is running there? A simple telnet from the host to the server at port 5432 should show you if it's reachable.

                  I think the point here is that open should cleanly fail and not trigger an application crash.

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

                  R C 2 Replies Last reply 16 Jul 2020, 20:29
                  2
                  • C Christian Ehrlicher
                    16 Jul 2020, 20:27

                    So did you actually check if the server is running there? A simple telnet from the host to the server at port 5432 should show you if it's reachable.

                    R Offline
                    R Offline
                    R-P-H
                    wrote on 16 Jul 2020, 20:29 last edited by
                    #8

                    @Christian-Ehrlicher said in QSqlDatabase open() crashes when db cannot be accessed ?:

                    So did you actually check if the server is running there? A simple telnet from the host to the server at port 5432 should show you if it's reachable.

                    No, it's purposely not reachable.

                    1 Reply Last reply
                    0
                    • S SGaist
                      16 Jul 2020, 20:28

                      @Christian-Ehrlicher said in QSqlDatabase open() crashes when db cannot be accessed ?:

                      So did you actually check if the server is running there? A simple telnet from the host to the server at port 5432 should show you if it's reachable.

                      I think the point here is that open should cleanly fail and not trigger an application crash.

                      R Offline
                      R Offline
                      R-P-H
                      wrote on 16 Jul 2020, 20:29 last edited by
                      #9

                      @SGaist said in QSqlDatabase open() crashes when db cannot be accessed ?:

                      @Christian-Ehrlicher said in QSqlDatabase open() crashes when db cannot be accessed ?:

                      So did you actually check if the server is running there? A simple telnet from the host to the server at port 5432 should show you if it's reachable.

                      I think the point here is that open should cleanly fail and not trigger an application crash.

                      Exactly, yes.

                      1 Reply Last reply
                      0
                      • S SGaist
                        16 Jul 2020, 20:28

                        @Christian-Ehrlicher said in QSqlDatabase open() crashes when db cannot be accessed ?:

                        So did you actually check if the server is running there? A simple telnet from the host to the server at port 5432 should show you if it's reachable.

                        I think the point here is that open should cleanly fail and not trigger an application crash.

                        C Offline
                        C Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 16 Jul 2020, 20:34 last edited by
                        #10

                        @SGaist said in QSqlDatabase open() crashes when db cannot be accessed ?:

                        I think the point here is that open should cleanly fail and not trigger an application crash.

                        It does not crash here with psql - the backtrace would be interesting.

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

                        R 1 Reply Last reply 16 Jul 2020, 20:34
                        1
                        • C Christian Ehrlicher
                          16 Jul 2020, 20:34

                          @SGaist said in QSqlDatabase open() crashes when db cannot be accessed ?:

                          I think the point here is that open should cleanly fail and not trigger an application crash.

                          It does not crash here with psql - the backtrace would be interesting.

                          R Offline
                          R Offline
                          R-P-H
                          wrote on 16 Jul 2020, 20:34 last edited by
                          #11

                          @Christian-Ehrlicher said in QSqlDatabase open() crashes when db cannot be accessed ?:

                          @SGaist said in QSqlDatabase open() crashes when db cannot be accessed ?:

                          I think the point here is that open should cleanly fail and not trigger an application crash.

                          It does not crash here with psql - the backtrace would be interesting.

                          Hi, how can I get that ?

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 16 Jul 2020, 20:36 last edited by
                            #12

                            Start your application in debug mode and you'll have it when it crashes through 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

                            R 1 Reply Last reply 16 Jul 2020, 20:43
                            2
                            • S SGaist
                              16 Jul 2020, 20:36

                              Start your application in debug mode and you'll have it when it crashes through the debugger.

                              R Offline
                              R Offline
                              R-P-H
                              wrote on 16 Jul 2020, 20:43 last edited by
                              #13

                              @SGaist said in QSqlDatabase open() crashes when db cannot be accessed ?:

                              Start your application in debug mode and you'll have it when it crashes through the debugger.

                              Ok, so very interesting, when I run the application in Debug mode it works as expected and does not crash...

                              C 1 Reply Last reply 16 Jul 2020, 20:58
                              0
                              • R R-P-H
                                16 Jul 2020, 20:43

                                @SGaist said in QSqlDatabase open() crashes when db cannot be accessed ?:

                                Start your application in debug mode and you'll have it when it crashes through the debugger.

                                Ok, so very interesting, when I run the application in Debug mode it works as expected and does not crash...

                                C Offline
                                C Offline
                                Cobra91151
                                wrote on 16 Jul 2020, 20:58 last edited by
                                #14

                                @R-P-H

                                Add the code below to the main.cpp file and try to run this code in both Debug and Release modes:

                                   qputenv("QT_FATAL_WARNINGS", "1");
                                   qSetMessagePattern("Type: %{type}\nProduct Name: %{appname}\nFile: %{file}\nLine: %{line}\nMethod: %{function}\nThreadID: %{threadid}\nThreadPtr: %{qthreadptr}\nMessage: %{message}");
                                

                                It should print some info about this crash issue.

                                R 1 Reply Last reply 16 Jul 2020, 21:07
                                0
                                • C Cobra91151
                                  16 Jul 2020, 20:58

                                  @R-P-H

                                  Add the code below to the main.cpp file and try to run this code in both Debug and Release modes:

                                     qputenv("QT_FATAL_WARNINGS", "1");
                                     qSetMessagePattern("Type: %{type}\nProduct Name: %{appname}\nFile: %{file}\nLine: %{line}\nMethod: %{function}\nThreadID: %{threadid}\nThreadPtr: %{qthreadptr}\nMessage: %{message}");
                                  

                                  It should print some info about this crash issue.

                                  R Offline
                                  R Offline
                                  R-P-H
                                  wrote on 16 Jul 2020, 21:07 last edited by
                                  #15

                                  @Cobra91151 said in QSqlDatabase open() crashes when db cannot be accessed ?:

                                  @R-P-H

                                  Add the code below to the main.cpp file and try to run this code in both Debug and Release modes:

                                     qputenv("QT_FATAL_WARNINGS", "1");
                                     qSetMessagePattern("Type: %{type}\nProduct Name: %{appname}\nFile: %{file}\nLine: %{line}\nMethod: %{function}\nThreadID: %{threadid}\nThreadPtr: %{qthreadptr}\nMessage: %{message}");
                                  

                                  It should print some info about this crash issue.

                                  Hi, nothing is printed. It is a widget application, not a console app if that's why ?

                                  C 1 Reply Last reply 16 Jul 2020, 21:15
                                  0
                                  • R R-P-H
                                    16 Jul 2020, 21:07

                                    @Cobra91151 said in QSqlDatabase open() crashes when db cannot be accessed ?:

                                    @R-P-H

                                    Add the code below to the main.cpp file and try to run this code in both Debug and Release modes:

                                       qputenv("QT_FATAL_WARNINGS", "1");
                                       qSetMessagePattern("Type: %{type}\nProduct Name: %{appname}\nFile: %{file}\nLine: %{line}\nMethod: %{function}\nThreadID: %{threadid}\nThreadPtr: %{qthreadptr}\nMessage: %{message}");
                                    

                                    It should print some info about this crash issue.

                                    Hi, nothing is printed. It is a widget application, not a console app if that's why ?

                                    C Offline
                                    C Offline
                                    Cobra91151
                                    wrote on 16 Jul 2020, 21:15 last edited by
                                    #16

                                    @R-P-H

                                    You need to clean, run qmake and rebuild your project. It should display the debug info when the crash occurs in the Application output window.

                                    R 1 Reply Last reply 16 Jul 2020, 21:30
                                    0
                                    • C Cobra91151
                                      16 Jul 2020, 21:15

                                      @R-P-H

                                      You need to clean, run qmake and rebuild your project. It should display the debug info when the crash occurs in the Application output window.

                                      R Offline
                                      R Offline
                                      R-P-H
                                      wrote on 16 Jul 2020, 21:30 last edited by
                                      #17

                                      @Cobra91151 said in QSqlDatabase open() crashes when db cannot be accessed ?:

                                      @R-P-H

                                      You need to clean, run qmake and rebuild your project. It should display the debug info when the crash occurs in the Application output window.

                                      Yes, I did as you said and there is no additional output. I also created a new console project with the same database code and it runs fine. No idea what's wrong with my widget application in release mode...

                                      1 Reply Last reply
                                      0
                                      • S Offline
                                        S Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on 17 Jul 2020, 19:00 last edited by
                                        #18

                                        One other thing you can do is set the QT_DEBUG_PLUGINS environment to 1 in the Run part of the Project panel to see if there's something happening there.

                                        Since you talked about a console app working fine, did you try to create a dummy widget project and just do the connection there ?

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

                                        R 1 Reply Last reply 17 Jul 2020, 20:48
                                        0
                                        • S SGaist
                                          17 Jul 2020, 19:00

                                          One other thing you can do is set the QT_DEBUG_PLUGINS environment to 1 in the Run part of the Project panel to see if there's something happening there.

                                          Since you talked about a console app working fine, did you try to create a dummy widget project and just do the connection there ?

                                          R Offline
                                          R Offline
                                          R-P-H
                                          wrote on 17 Jul 2020, 20:48 last edited by
                                          #19

                                          @SGaist said in QSqlDatabase open() crashes when db cannot be accessed ?:

                                          One other thing you can do is set the QT_DEBUG_PLUGINS environment to 1 in the Run part of the Project panel to see if there's something happening there.

                                          Since you talked about a console app working fine, did you try to create a dummy widget project and just do the connection there ?

                                          I set the environment variable. I get the output from all the plugins on startup but nothing extra when calling open().

                                          I created a fresh widget project and just added the connection part of the code and it works fine. I figure that this means there's some other library included in my original application that may be causing this issue ?

                                          1 Reply Last reply
                                          0
                                          • R Offline
                                            R Offline
                                            R-P-H
                                            wrote on 17 Jul 2020, 21:31 last edited by
                                            #20

                                            So through much debugging I finally found the issue. Just 1 line of code:

                                            QCoreApplication::processEvents();
                                            

                                            I have a thread that runs at application startup and emits an image to a label for displaying. The above line of code was at the end of this loop. Removing the line of code doesn't appear to cause any other issues and solves the segfault when trying to open the database.

                                            Why this causes the segfault in the first place is still bizarre to me...

                                            1 Reply Last reply
                                            0

                                            1/27

                                            16 Jul 2020, 18:59

                                            • Login

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