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. QSql Database Segment Fault
Forum Update on Monday, May 27th 2025

QSql Database Segment Fault

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 857 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.
  • C Offline
    C Offline
    Chrisw01
    wrote on last edited by
    #1

    Hello, I've been racking my head on this one for a while now... Most of the code is taken from the Qt Documentation.

    void morpheous::loadExcel()
    {
        QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", "xlsx_connection");
    
        db.setDatabaseName("DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" + QString("c:\\Stock_List.xlsx"));
        if(db.open()) {
            QSqlQuery sql(db);
            sql.exec("BEGIN");
            sql.exec("select * from [" + QString("Stock_List") + "$A7:AH2000]");
            if(sql.isActive()) {
                while(sql.next()) {
                    // Will place proper values into our database later.
                    qDebug() << sql.value(0).toString() << " " << sql.value(2).toString() << " " << sql.value(7).toString();
                }
                sql.exec("END");
            }
            else {
                qDebug() << sql.lastError().text();
            }
            db.close();
            QSqlDatabase::removeDatabase("xlsx_connection"); // Segfaults if I leave this line in...
        }
        else qDebug() << db.lastError().text();
    }
    

    If I leave the removeDatabase line in there the program segfaults. If I comment it out then I get a warning that the database is a duplicate and will be removed.

    Any suggestions?

    Thanks

    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What version of Qt ?
      On what OS ?
      Can you show the stack trace ?

      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
      • C Offline
        C Offline
        Chrisw01
        wrote on last edited by
        #3

        Darn it I always forget that...

        I'm on windows 10, Qt 5.11.3 using mingw53_32.

        1  QODBCResultPrivate::isStmtHandleValid                            qsql_odbc.cpp                  229  0x678c1df1 
        2  QODBCResult::~QODBCResult                                        qsql_odbc.cpp                  975  0x678c6239 
        3  QODBCResult::~QODBCResult                                        qsql_odbc.cpp                  981  0x678c6309 
        4  QSqlQueryPrivate::~QSqlQueryPrivate                              qsqlquery.cpp                  94   0x67b42ee3 
        5  QSqlQuery::~QSqlQuery                                            qsqlquery.cpp                  245  0x67b42f2f 
        6  morpheous::loadExcel                                             morpheous.cpp                  31   0x40b2e7   
        7  morpheous::qt_static_metacall                                    moc_morpheous.cpp              148  0x412454   
        8  QMetaObject::activate                                            qobject.cpp                    3771 0x6bb1c379 
        9  QMetaObject::activate                                            qobject.cpp                    3633 0x6bb1c60d 
        10 QAbstractButton::clicked                                         moc_qabstractbutton.cpp        308  0x130fb688 
        11 QAbstractButtonPrivate::emitClicked                              qabstractbutton.cpp            414  0x130fb8b1 
        12 QAbstractButtonPrivate::click                                    qabstractbutton.cpp            407  0x130fd056 
        13 QAbstractButton::mouseReleaseEvent                               qabstractbutton.cpp            1011 0x130fd286 
        14 QWidget::event                                                   qwidget.cpp                    8907 0x1304b0a8 
        15 QAbstractButton::event                                           qabstractbutton.cpp            968  0x130fe698 
        16 QPushButton::event                                               qpushbutton.cpp                684  0x131935cd 
        17 QApplicationPrivate::notify_helper                               qapplication.cpp               3726 0x130085ca 
        18 QApplication::notify                                             qapplication.cpp               3202 0x13010107 
        19 QCoreApplication::notifyInternal2                                qcoreapplication.cpp           1047 0x6baf51b1 
        20 QCoreApplication::sendSpontaneousEvent                           qcoreapplication.h             237  0x1300f077 
        21 QApplicationPrivate::sendMouseEvent                              qapplication.cpp               2692 0x1300f077 
        22 QWidgetWindow::handleMouseEvent                                  qwidgetwindow.cpp              660  0x13061abc 
        23 QWidgetWindow::event                                             qwidgetwindow.cpp              281  0x13063bc1 
        24 QApplicationPrivate::notify_helper                               qapplication.cpp               3726 0x130085ca 
        25 QApplication::notify                                             qapplication.cpp               3098 0x1300fb83 
        26 QCoreApplication::notifyInternal2                                qcoreapplication.cpp           1047 0x6baf51b1 
        27 QCoreApplication::sendSpontaneousEvent                           qcoreapplication.h             237  0x163acc6  
        28 QGuiApplicationPrivate::processMouseEvent                        qguiapplication.cpp            2082 0x163acc6  
        29 QGuiApplicationPrivate::processWindowSystemEvent                 qguiapplication.cpp            1817 0x163c5a1  
        30 QWindowSystemInterface::sendWindowSystemEvents                   qwindowsysteminterface.cpp     1032 0x161df91  
        31 QWindowsGuiEventDispatcher::sendPostedEvents                     qwindowsguieventdispatcher.cpp 82   0x1edfdd94 
        32 qt_internal_proc(HWND__ *, unsigned int, unsigned int, long) *16 qeventdispatcher_win.cpp       237  0x6bb454c5 
        33 USER32!AddClipboardFormatListener                                                                    0x763c48eb 
        34 USER32!CallWindowProcW                                                                               0x763a613c 
        35 USER32!DispatchMessageW                                                                              0x763a528e 
        36 USER32!DispatchMessageW                                                                              0x763a5070 
        37 QEventDispatcherWin32::processEvents                             qeventdispatcher_win.cpp       629  0x6bb44c20 
        38 QWindowsGuiEventDispatcher::processEvents                        qwindowsguieventdispatcher.cpp 74   0x1edfdd67 
        39 QEventLoop::processEvents                                        qeventloop.cpp                 136  0x6baf3860 
        40 QEventLoop::exec                                                 qeventloop.cpp                 214  0x6baf3cb8 
        41 QCoreApplication::exec                                           qcoreapplication.cpp           1335 0x6bafc3a6 
        42 QGuiApplication::exec                                            qguiapplication.cpp            1762 0x16315be  
        43 QApplication::exec                                               qapplication.cpp               2900 0x130084a9 
        44 qMain                                                            main.cpp                       14   0x409e6b   
        45 WinMain *16                                                      qtmain_win.cpp                 104  0x413dd5   
        46 main                                                                                                 0x4247fd   
        
        

        Thanks

        1 Reply Last reply
        0
        • C Chrisw01

          Hello, I've been racking my head on this one for a while now... Most of the code is taken from the Qt Documentation.

          void morpheous::loadExcel()
          {
              QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", "xlsx_connection");
          
              db.setDatabaseName("DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" + QString("c:\\Stock_List.xlsx"));
              if(db.open()) {
                  QSqlQuery sql(db);
                  sql.exec("BEGIN");
                  sql.exec("select * from [" + QString("Stock_List") + "$A7:AH2000]");
                  if(sql.isActive()) {
                      while(sql.next()) {
                          // Will place proper values into our database later.
                          qDebug() << sql.value(0).toString() << " " << sql.value(2).toString() << " " << sql.value(7).toString();
                      }
                      sql.exec("END");
                  }
                  else {
                      qDebug() << sql.lastError().text();
                  }
                  db.close();
                  QSqlDatabase::removeDatabase("xlsx_connection"); // Segfaults if I leave this line in...
              }
              else qDebug() << db.lastError().text();
          }
          

          If I leave the removeDatabase line in there the program segfaults. If I comment it out then I get a warning that the database is a duplicate and will be removed.

          Any suggestions?

          Thanks

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

          @Chrisw01
          I'm not defending whether it's correct behaviour or not, but from your stack trace clearly what is happening is that when your QSqlQuery sql(db); goes out of scope and so calls the destructor the internal code is not happy that your database has been removed. If you move your removeDatabase() call to outside the scope of your QSqlQuery sql(db); so that gets destructed before removal, I presume the seg fault goes away?

          1 Reply Last reply
          2
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by Christian Ehrlicher
            #5

            @Chrisw01 said in QSql Database Segment Fault:

            QSqlDatabase::removeDatabase("xlsx_connection"); // Segfaults if I leave this line in...

            At this place you've still an open QSqlQuery as @JonB already said. You will get a warning on the console about this. But it should not crash. A fix for a similar crash was recently added for QPSQL (QTBUG-43889). Added a bugreport.

            /edit: here the patch: https://codereview.qt-project.org/c/qt/qtbase/+/276419

            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
            4
            • C Offline
              C Offline
              Chrisw01
              wrote on last edited by
              #6

              Thanks guys, you put me on the right track. Both sql and db must go out of Scope before calling removeDatabase(); I found some references on the Internet about this. So I wrapped my code in a couple of brackets before calling removeDatabase(); and all issues and warnings went away...

              Thanks again!!!!

              1 Reply Last reply
              2
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Then please mark the topic as solved, thx :)

                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
                0

                • Login

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