Results 1 to 2 of 2

Thread: QSqlQuery Memory issues. QSqlQuery::exec() and QSqlDatabase::open()/close();

  1. #1
    Join Date
    Aug 2010
    Posts
    30
    Thanks
    2

    Default QSqlQuery Memory issues. QSqlQuery::exec() and QSqlDatabase::open()/close();

    Hi,

    I'm checking the memory usage of an application I've made. It makes numerous calls to read and write values to and from a database. I've observed the following:

    * QSqlQuery::exec() uses some KB of RAM to execute a given query, but does not release the memory after it goes out of scope.

    * QSqlDatabase:: open() & close() do not help free resources as the documentation suggest. If anything, close() causes resources (at least memory) to remain 'trapped' on the heap/stack.

    For example, here is a typical segment of code I've been using to access my database.

    Qt Code:
    1. QStringList values;
    2. db.open();
    3. QString strQuery = "SELECT DISTINCT " + field + " FROM " + table + str;
    4.  
    5. QSqlQuery query(db);
    6. query.prepare(strQuery);
    7.  
    8. if(query.exec() == true)
    9. {
    10. while(query.next())
    11. {
    12. values.push_back(query.value(0).toString());
    13. }
    14. }
    15.  
    16. db.close();
    To copy to clipboard, switch view to plain text mode 


    Having experimented with I find the code below 'traps' less memory:

    Qt Code:
    1. QStringList values;
    2. QString strQuery = "SELECT DISTINCT " + field + " FROM " + table + str;
    3.  
    4. QSqlQuery query(strQuery, db);
    5.  
    6. while(query.next())
    7. {
    8. values.push_back(query.value(0).toString());
    9. }
    To copy to clipboard, switch view to plain text mode 

    However, a small amount of memory is still not released. Has anyone else experienced anything like this? Comments?

  2. #2
    Join Date
    Aug 2010
    Posts
    30
    Thanks
    2

    Default Re: QSqlQuery Memory issues. QSqlQuery::exec() and QSqlDatabase::open()/close();

    It seems that in order to release this memory you must create the QSqlQuery variable as a pointer, and delete this pointer before you close the database:

    Qt Code:
    1. QStringList values;
    2. db.open();
    3. QString strQuery = "SELECT DISTINCT " + field + " FROM " + table + str;
    4.  
    5. QSqlQuery *query = new QSqlQuery(db);
    6. query->prepare(strQuery);
    7.  
    8. if(query->exec() == true)
    9. {
    10. while(query->next())
    11. {
    12. values.push_back(query->value(0).toString());
    13. }
    14. }
    15.  
    16. delete query;
    17. db.close();
    To copy to clipboard, switch view to plain text mode 

    The memory is then released after the database closes.

Similar Threads

  1. Abort QSqlQuery::exec()
    By elmo in forum Qt Programming
    Replies: 5
    Last Post: 13th October 2013, 20:50
  2. Replies: 1
    Last Post: 1st August 2010, 14:06
  3. QSqlQuery.exec() weird error
    By MarkoSan in forum Qt Programming
    Replies: 3
    Last Post: 25th May 2010, 13:02
  4. QSqlQuery and no response while exec()
    By jacek_ in forum Qt Programming
    Replies: 5
    Last Post: 5th November 2009, 08:47
  5. QSqlQuery::exec: database not open
    By newtowindows in forum Qt Programming
    Replies: 8
    Last Post: 29th October 2009, 08:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.