I have written the below code in order to have concurrent queries.
QString databaseName = "DB-"+QThread::currentThread()->objectName();
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL",databaseName);
db.setHostName("192.168.1.2");
db.setDatabaseName("Behroozi");
db.setUserName("root");
db.setPassword("password");
db.open();
QSqlQuery query(db);
query.prepare("select * from Person where chatID=:chatID");
query.bindValue(":chatID",chatID);
if(query.exec())
return true;
db.close();
QSqlDatabase::removeDatabase(databaseName);
The code creates a connection with unique name, opens it, make query, closes it and at last removes the connection.
The problem is, it has memory leakage. When I comment the code, it does not leak. What else should I do in order to prevent it?
Is it a safe code I have written?
If I copy db.close() and removeDatabase before return, it will print:
QSqlDatabasePrivate::removeDatabase: connection 'DB-ReplyThread-1' is still in use, all queries will cease to work.