QSqlDatabasePrivate::removeDatabase
-
Got it, but where do you call MysqlConnector2::close() (outside this class).
Does the error message appear when you close your app, or when you call MysqlConnector2::close() method ? (verify this point using debug mode and breakpoints !)
I recommand you to make atomic methods that implie to open AND close connection to your DB. By this way, you will be sure release your resources.
See the EDIT in my previous message.
-
Here i call the close():
@ conn->close();
logger->close();
logger->logOnFile("MainButton","mousePressEvent","Exiting from program.");
exit(0);
@just before exiting...
-
I do this:
@
db->close();
delete db;
QSqlDatabase::removeDatabase(dbName);
@ -
Thanks for the suggestion!
Doing like this it works:
@bool MysqlConnector2::close()
{
bool result=true;QString connection; connection=db.connectionName(); db.close(); delete &db; QSqlDatabase::removeDatabase(connection); return result;
}@
-
delete &db is really aehm... ugly! You are calling the destructor on a stack-allocated object. This will lead to the destructor of db getting called twice, once when you do it and once when it goes out of scope. Undefined behaviour is lurking here... nothing I want close to my databases;-)
You could hold a pointer to a db instead, initializing it like this db = new QSqlDatabase(QSqlDatabase::addDatabase(...));
That pointer can be deleted properly in your close method.
-
You are right,
that was only an attempt.
Now i will modify the code to be less ugly..eheheh
-
finally, how do you solve the problem?
-
Nice, it works for me too, just by declaring a pointer instead of an object and using it like this:
@
m_db = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE"));
m_szConnectionName = m_db->connectionName();
...
m_db->open();
...
m_db->close();
...
delete m_db
QSqlDatabase::removeDatabase(m_szConnectionName);
@ -
how do you define m_db?
-
ok i found it
QSqlDatabase *db;thanks it worked for me