PDA

View Full Version : QSqlQuery Memory issues. QSqlQuery::exec() and QSqlDatabase::open()/close();



johnnyturbo3
13th July 2011, 16:10
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.


QStringList values;
db.open();
QString strQuery = "SELECT DISTINCT " + field + " FROM " + table + str;

QSqlQuery query(db);
query.prepare(strQuery);

if(query.exec() == true)
{
while(query.next())
{
values.push_back(query.value(0).toString());
}
}

db.close();


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



QStringList values;
QString strQuery = "SELECT DISTINCT " + field + " FROM " + table + str;

QSqlQuery query(strQuery, db);

while(query.next())
{
values.push_back(query.value(0).toString());
}


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

johnnyturbo3
18th July 2011, 13:12
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:


QStringList values;
db.open();
QString strQuery = "SELECT DISTINCT " + field + " FROM " + table + str;

QSqlQuery *query = new QSqlQuery(db);
query->prepare(strQuery);

if(query->exec() == true)
{
while(query->next())
{
values.push_back(query->value(0).toString());
}
}

delete query;
db.close();


The memory is then released after the database closes.