PDA

View Full Version : QSqlDatabase error



maxpower
17th October 2006, 21:47
I am getting the following error displayed in a console when I close my database program:

QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.

I have issued db.close() as part of my destructor but this message persists.

Any ideas?

mAx

jacek
17th October 2006, 23:04
Maybe some instances of QSqlQuery, QSqlQueryModel, QSqlTableModel and similar classes still exist?

maxpower
18th October 2006, 15:36
I might be missing something because I did have a QSqlTableModel open but I used clear and still receive the error. I have attached my code in hopes that someone can find the error.

mAx

jacek
18th October 2006, 21:04
I did have a QSqlTableModel open but I used clear and still receive the error.
clear() should be enough, but the problem is that you have many such models and you clear only one of them. You have a memory leak --- each time loadTableView() is invoked, you create a new model and you never delete it.

maxpower
18th October 2006, 21:52
Okay, I added a "delete model" to each radio button's viewInit logic so that if model already has been initialized it is delete before creating a new one:


if( toggled == true )
{
if(viewInit)
{
model->submitAll();
delete model;
}
tableName = "vacation";
loadTableView(tableName);
ui.newPicPB->setEnabled(true);
ui.movePB->setEnabled(true);
}


However, model-clear() still does not release the only existing model when the program closes as I still receive the error.

Thanks for the help
mAx

jacek
19th October 2006, 13:53
Okay, I added a "delete model" to each radio button's viewInit logic
I would add it to loadTableView(), since you invoke it in many places.

Why do you create a new model in destructor? You won't be able to delete it and you'll get a memory leak. Same thing in closer().

You have a lot of duplicated code. For example on_matthewRB_toggled(), on_joshuaRB_toggled() and on_vacationsRB_toggled() are exactly the same and you can use just one method instead. Also compare on_settingsPB_clicked() and Init().

maxpower
19th October 2006, 16:02
Ok, good thinking. Here is my revised code. However, I still get the error even though there should only be one model open at a time. is the ui.tableTV the problem? I really appreciate your help.

mAx

maxpower
19th October 2006, 19:00
Well, I finally got it. I moved my model->submitAll() from the destructor to closer() and then replaced the model->clear() there with delete model. I also moved db.close() to closer(). Guess I still don't now why clear() doesn't work, but hey, it works. Thanks for all your help.

mAx