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. Why does the app crashes on delete QSqlQuery*
Forum Updated to NodeBB v4.3 + New Features

Why does the app crashes on delete QSqlQuery*

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 5 Posters 1.1k Views 3 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.
  • D Offline
    D Offline
    deleted385
    wrote on last edited by deleted385
    #1

    Here's what I've now:

    void ObjectsView::openFileDialog(){
        QFileDialog dialog;
        if(!dialog.exec()) return;
        if(db.isOpen()) {
            db.close();
            qDebug() << "deleting";
            delete query;
            qDebug() << "deleted";
        }
        auto widget = qobject_cast<QueryWidget*>(parent()->parent());
        fileName = dialog.selectedFiles().first();
        db.setDatabaseName(fileName);
        db.open();
        query = new QSqlQuery("SELECT * FROM sqlite_master", db);
        query->setForwardOnly(true);
        if(!query->exec()){
            emit widget->logMessage("e,Could not connect to " + fileName); //e, for error
            return;
        }
        makeTree();
        emit widget->logMessage("s,Connected to " + fileName); // s, for success
        isConnected = true;
    }
    

    With these it doesn't crash always. When it does, I see it printing deleting, so it crashes for this delete query line. How to fix?

    Pl45m4P JonBJ eyllanescE 3 Replies Last reply
    0
    • D deleted385

      Here's what I've now:

      void ObjectsView::openFileDialog(){
          QFileDialog dialog;
          if(!dialog.exec()) return;
          if(db.isOpen()) {
              db.close();
              qDebug() << "deleting";
              delete query;
              qDebug() << "deleted";
          }
          auto widget = qobject_cast<QueryWidget*>(parent()->parent());
          fileName = dialog.selectedFiles().first();
          db.setDatabaseName(fileName);
          db.open();
          query = new QSqlQuery("SELECT * FROM sqlite_master", db);
          query->setForwardOnly(true);
          if(!query->exec()){
              emit widget->logMessage("e,Could not connect to " + fileName); //e, for error
              return;
          }
          makeTree();
          emit widget->logMessage("s,Connected to " + fileName); // s, for success
          isConnected = true;
      }
      

      With these it doesn't crash always. When it does, I see it printing deleting, so it crashes for this delete query line. How to fix?

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by
      #6

      @Emon-Haque Why is a pointer necessary? Change QSqlQuery *query to QSqlQuery query in .h, and then make query = QSqlQuery("SELECT * FROM sqlite_master", db);, thus avoiding these unnecessary problems.

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      D 1 Reply Last reply
      2
      • D deleted385

        Here's what I've now:

        void ObjectsView::openFileDialog(){
            QFileDialog dialog;
            if(!dialog.exec()) return;
            if(db.isOpen()) {
                db.close();
                qDebug() << "deleting";
                delete query;
                qDebug() << "deleted";
            }
            auto widget = qobject_cast<QueryWidget*>(parent()->parent());
            fileName = dialog.selectedFiles().first();
            db.setDatabaseName(fileName);
            db.open();
            query = new QSqlQuery("SELECT * FROM sqlite_master", db);
            query->setForwardOnly(true);
            if(!query->exec()){
                emit widget->logMessage("e,Could not connect to " + fileName); //e, for error
                return;
            }
            makeTree();
            emit widget->logMessage("s,Connected to " + fileName); // s, for success
            isConnected = true;
        }
        

        With these it doesn't crash always. When it does, I see it printing deleting, so it crashes for this delete query line. How to fix?

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #2

        @Emon-Haque said in Why does the app crashes on delete QSqlQuery*:

        When it does, I see it printing deleting, so it crashes for this delete query line. How to fix?

        Looks like a segfault. You're probably accessing query (i.e. trying to delete it) when there wasn't created a query object before, so it's still null.

        Check if query is valid before trying to delete it.


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        D 1 Reply Last reply
        1
        • D deleted385

          Here's what I've now:

          void ObjectsView::openFileDialog(){
              QFileDialog dialog;
              if(!dialog.exec()) return;
              if(db.isOpen()) {
                  db.close();
                  qDebug() << "deleting";
                  delete query;
                  qDebug() << "deleted";
              }
              auto widget = qobject_cast<QueryWidget*>(parent()->parent());
              fileName = dialog.selectedFiles().first();
              db.setDatabaseName(fileName);
              db.open();
              query = new QSqlQuery("SELECT * FROM sqlite_master", db);
              query->setForwardOnly(true);
              if(!query->exec()){
                  emit widget->logMessage("e,Could not connect to " + fileName); //e, for error
                  return;
              }
              makeTree();
              emit widget->logMessage("s,Connected to " + fileName); // s, for success
              isConnected = true;
          }
          

          With these it doesn't crash always. When it does, I see it printing deleting, so it crashes for this delete query line. How to fix?

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #3

          @Emon-Haque
          As @Pl45m4 has commented. Your query is a member variable, so what do you initialise it to before you call this method for the first time?!

          Also, after you go delete query; please set it to nullptr, to avoid any future re-deletes....

          D 1 Reply Last reply
          0
          • Pl45m4P Pl45m4

            @Emon-Haque said in Why does the app crashes on delete QSqlQuery*:

            When it does, I see it printing deleting, so it crashes for this delete query line. How to fix?

            Looks like a segfault. You're probably accessing query (i.e. trying to delete it) when there wasn't created a query object before, so it's still null.

            Check if query is valid before trying to delete it.

            D Offline
            D Offline
            deleted385
            wrote on last edited by deleted385
            #4

            @Pl45m4, in the constructor, I don't do anything with the query, for the db, I have only one line:

            db = QSqlDatabase::addDatabase("QSQLITE");
            

            I changed that qDebug() << "deleting"; to qDebug() << "deleting" << query;. Here's, without a crash, what I did:

            x1.gif

            and here's the qDebug output:

            deleting 0x0
            deleted
            deleting 0x41a5c00
            deleted
            deleting 0x41a59e0
            deleted
            

            first, I attached quran.db and it deleted a nullptr? I didn't call db.Open() anywhere, this is the only function where I call db.Open(). Looks like db.isOpen() returned true although I didn't call Open(). For the rest two database I attached, it got something to delete.

            1 Reply Last reply
            0
            • JonBJ JonB

              @Emon-Haque
              As @Pl45m4 has commented. Your query is a member variable, so what do you initialise it to before you call this method for the first time?!

              Also, after you go delete query; please set it to nullptr, to avoid any future re-deletes....

              D Offline
              D Offline
              deleted385
              wrote on last edited by
              #5

              @JonB, do I've to initialize it in constructor? I check whether the db.isOpen(), if it is opened, it should get into that block BUT, to me, it looks like it's getting into that block when the db is not open.

              JonBJ 1 Reply Last reply
              0
              • D deleted385

                Here's what I've now:

                void ObjectsView::openFileDialog(){
                    QFileDialog dialog;
                    if(!dialog.exec()) return;
                    if(db.isOpen()) {
                        db.close();
                        qDebug() << "deleting";
                        delete query;
                        qDebug() << "deleted";
                    }
                    auto widget = qobject_cast<QueryWidget*>(parent()->parent());
                    fileName = dialog.selectedFiles().first();
                    db.setDatabaseName(fileName);
                    db.open();
                    query = new QSqlQuery("SELECT * FROM sqlite_master", db);
                    query->setForwardOnly(true);
                    if(!query->exec()){
                        emit widget->logMessage("e,Could not connect to " + fileName); //e, for error
                        return;
                    }
                    makeTree();
                    emit widget->logMessage("s,Connected to " + fileName); // s, for success
                    isConnected = true;
                }
                

                With these it doesn't crash always. When it does, I see it printing deleting, so it crashes for this delete query line. How to fix?

                eyllanescE Offline
                eyllanescE Offline
                eyllanesc
                wrote on last edited by
                #6

                @Emon-Haque Why is a pointer necessary? Change QSqlQuery *query to QSqlQuery query in .h, and then make query = QSqlQuery("SELECT * FROM sqlite_master", db);, thus avoiding these unnecessary problems.

                If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

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

                  Hi,

                  @Emon-Haque said in Why does the app crashes on delete QSqlQuery*:

                  How to fix?

                  In the case of QSqlQuery: do not create it on the heap. There's no reason for that.
                  If you look at all the code related to QSqlQuery, you can see that none of them allocate it on the heap.

                  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
                  1
                  • eyllanescE eyllanesc

                    @Emon-Haque Why is a pointer necessary? Change QSqlQuery *query to QSqlQuery query in .h, and then make query = QSqlQuery("SELECT * FROM sqlite_master", db);, thus avoiding these unnecessary problems.

                    D Offline
                    D Offline
                    deleted385
                    wrote on last edited by deleted385
                    #8

                    @eyllanesc, can you change the database of such query variable later?

                    EDIT
                    I did try that approach and at that time I got some error. Now, I don't have any, I'll go with that, Thanks.

                    eyllanescE 1 Reply Last reply
                    0
                    • D deleted385

                      @eyllanesc, can you change the database of such query variable later?

                      EDIT
                      I did try that approach and at that time I got some error. Now, I don't have any, I'll go with that, Thanks.

                      eyllanescE Offline
                      eyllanescE Offline
                      eyllanesc
                      wrote on last edited by
                      #9

                      @Emon-Haque Your question is unclear. If you don't pass a QSqlDatabase to QSqlQuery then it will use the default database.

                      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                      D 1 Reply Last reply
                      0
                      • eyllanescE eyllanesc

                        @Emon-Haque Your question is unclear. If you don't pass a QSqlDatabase to QSqlQuery then it will use the default database.

                        D Offline
                        D Offline
                        deleted385
                        wrote on last edited by
                        #10

                        @eyllanesc, it can't be changed later. I've to reassign query = QSqlQuery("SELECT * FROM sqlite_master", db); everytime. Not a problem, I think.

                        JonBJ 1 Reply Last reply
                        0
                        • D deleted385

                          @JonB, do I've to initialize it in constructor? I check whether the db.isOpen(), if it is opened, it should get into that block BUT, to me, it looks like it's getting into that block when the db is not open.

                          JonBJ Online
                          JonBJ Online
                          JonB
                          wrote on last edited by
                          #11

                          @Emon-Haque
                          In your code, if when you enter ObjectsView::openFileDialog() db is already open but query is a member variable and uninitialised it will crash. Initialise member variables before first use.

                          I also said that for safety you should query = nullptr immediately after delete query, to prevent accidental re-deletion.

                          If you delete it anywhere else it will also crash here.

                          Finally, if nothing works. I don't know about this, but when you create query you associate it with db at that time. I do not know if you close or change db whether that could cause a problem when you later delete query associated with a now-closed-or-different db.

                          Frankly the whole business of deleting query only next time you open/create db is "messy". And it doesn't solve what happens to allocated query if you never come to openFileDialog() again. Do you need to do things this way?

                          1 Reply Last reply
                          2
                          • D deleted385

                            @eyllanesc, it can't be changed later. I've to reassign query = QSqlQuery("SELECT * FROM sqlite_master", db); everytime. Not a problem, I think.

                            JonBJ Online
                            JonBJ Online
                            JonB
                            wrote on last edited by JonB
                            #12

                            @Emon-Haque said in Why does the app crashes on delete QSqlQuery*:

                            @eyllanesc, it can't be changed later. I've to reassign query = QSqlQuery("SELECT * FROM sqlite_master", db); everytime. Not a problem, I think.

                            Just what is waiting to trip someone up when the code gets changed at a later date. Things like "set to nullptr after you delete a member variable" is just good practice.

                            D 1 Reply Last reply
                            1
                            • JonBJ JonB

                              @Emon-Haque said in Why does the app crashes on delete QSqlQuery*:

                              @eyllanesc, it can't be changed later. I've to reassign query = QSqlQuery("SELECT * FROM sqlite_master", db); everytime. Not a problem, I think.

                              Just what is waiting to trip someone up when the code gets changed at a later date. Things like "set to nullptr after you delete a member variable" is just good practice.

                              D Offline
                              D Offline
                              deleted385
                              wrote on last edited by deleted385
                              #13

                              @JonB, later after a break, I'll try with a global QSqlQuery* again and will try to use that in other view, where I've used QSqlQueryModel testModel (in previous post), to execute multiple statements AND instead of db.isOpen(), I'll use my global isConnected to see whether it crashes with that or not.

                              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