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 - memory leak
Forum Update on Monday, May 27th 2025

QSqlDatabase - memory leak

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 1.8k 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.
  • T Offline
    T Offline
    Tancen
    wrote on 22 Jan 2018, 07:52 last edited by Tancen
    #1

    qt 5.7 mingw 32bits

    #include <qcoreapplication.h>
    #include <QSqlDatabase>
    #include <qdebug.h>
    #include <quuid.h>
    #include <qsqldatabase.h>
    #include <qsqlquery.h>
    
    bool gExit = false;
    
    void handleSignal(int sig)
    {
        gExit = true;
    }
    
    void test1()
    {
        QString name = QUuid::createUuid().toString();
        QSqlDatabase *db = new QSqlDatabase(QSqlDatabase::addDatabase("QOCI", name));
        db->setDatabaseName("test");
        db->setHostName("localhost");
        db->setPort(1521);
        db->setUserName("system");
        db->setPassword("123456");
    
        for (int i = 0; i < 1000; ++i)
        {
            if(!db->open())
            {
                qDebug("db->open failed");
                delete db;
                return;
            }
    
            db->close();
            qDebug("done %d", i);
        }
        delete db;
        //QSqlDatabase::removeDatabase(name); //unless enable this
    }
    
    int main(int argc, char *argv[])
    {
        signal(SIGINT, handleSignal);
        QCoreApplication app(argc, argv);
    
        test1();
    
        int ret = app.exec();
        return ret;
    }
    
    

    memory malloc in db.open, but not free in db.close.

    J 1 Reply Last reply 22 Jan 2018, 09:56
    0
    • T Tancen
      22 Jan 2018, 07:52

      qt 5.7 mingw 32bits

      #include <qcoreapplication.h>
      #include <QSqlDatabase>
      #include <qdebug.h>
      #include <quuid.h>
      #include <qsqldatabase.h>
      #include <qsqlquery.h>
      
      bool gExit = false;
      
      void handleSignal(int sig)
      {
          gExit = true;
      }
      
      void test1()
      {
          QString name = QUuid::createUuid().toString();
          QSqlDatabase *db = new QSqlDatabase(QSqlDatabase::addDatabase("QOCI", name));
          db->setDatabaseName("test");
          db->setHostName("localhost");
          db->setPort(1521);
          db->setUserName("system");
          db->setPassword("123456");
      
          for (int i = 0; i < 1000; ++i)
          {
              if(!db->open())
              {
                  qDebug("db->open failed");
                  delete db;
                  return;
              }
      
              db->close();
              qDebug("done %d", i);
          }
          delete db;
          //QSqlDatabase::removeDatabase(name); //unless enable this
      }
      
      int main(int argc, char *argv[])
      {
          signal(SIGINT, handleSignal);
          QCoreApplication app(argc, argv);
      
          test1();
      
          int ret = app.exec();
          return ret;
      }
      
      

      memory malloc in db.open, but not free in db.close.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 22 Jan 2018, 09:56 last edited by
      #2

      @Tancen It would be nice to describe the problem instead simply throwing some code...
      You don't delete db if you do a return in

      if(!db->open())
      {
          qDebug("db->open failed");
          return;
      }
      

      Why do you create db on the stack?

      QSqlDatabase db;
      

      would be enough.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • T Offline
        T Offline
        Tancen
        wrote on 22 Jan 2018, 12:10 last edited by
        #3

        i forget it. but the problem is not about it. you can try and repeat open / close more times.

        T 1 Reply Last reply 22 Jan 2018, 12:20
        0
        • T Tancen
          22 Jan 2018, 12:10

          i forget it. but the problem is not about it. you can try and repeat open / close more times.

          T Offline
          T Offline
          Taz742
          wrote on 22 Jan 2018, 12:20 last edited by Taz742
          #4

          @Tancen

          @Tancen said in QSqlDatabase - memory leak:

          you can try and repeat open / close more times.

          He knows it.

          It tells you that if the base is not opened, you're doing 'return' and this is exactly the problem, 'return' - has finished function and the base itself will not be removed.

              for (int i = 0; i < 1000; ++i)
              {
                  if(!db->open())
                  {
                      qDebug("db->open failed");
                      delete db;
                      return; //stop function
                  }
          
                  db->close();
                  qDebug("done %d", i);
              }
              delete db; //It will not be fulfilled.
          

          Do what you want.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            Tancen
            wrote on 22 Jan 2018, 12:31 last edited by Tancen
            #5

            thanks.
            i know he means.
            i tested and get memory leak about open/close
            my code is only a mini show

            J 1 Reply Last reply 22 Jan 2018, 12:33
            0
            • T Tancen
              22 Jan 2018, 12:31

              thanks.
              i know he means.
              i tested and get memory leak about open/close
              my code is only a mini show

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 22 Jan 2018, 12:33 last edited by
              #6

              @Tancen How do you know you have a memory leak?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • T Offline
                T Offline
                Tancen
                wrote on 22 Jan 2018, 12:40 last edited by
                #7

                i repeat the for 1000 times, i see the memory increase in windows task manager and not decrease on the for finished

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 22 Jan 2018, 12:51 last edited by
                  #8

                  Hi,

                  No OS guaranties that when you call delete, the memory is returned instantly to said OS. All the more if you are using a tight loop like that.

                  Note that you are also not using the QSqlDatabase API correctly.

                  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
                  3
                  • T Offline
                    T Offline
                    Tancen
                    wrote on 22 Jan 2018, 12:57 last edited by
                    #9

                    it's same as wait it some time.
                    i tested the memory free at QSqlDatabase::removeDatabase be called

                    1 Reply Last reply
                    0

                    1/9

                    22 Jan 2018, 07:52

                    • Login

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