Safely close database connections and delete backends

Also fix NewClosure leak caused by disconnected object signals
This commit is contained in:
Jonas Kvinge
2019-07-24 19:16:51 +02:00
parent bd78e8c275
commit b5eb13449b
47 changed files with 490 additions and 53 deletions

View File

@@ -211,6 +211,7 @@ int Database::FTSNext(sqlite3_tokenizer_cursor *cursor, const char* *token, int
void Database::StaticInit() {
if (sFTSTokenizer) return;
sFTSTokenizer = new sqlite3_tokenizer_module;
sFTSTokenizer->iVersion = 0;
sFTSTokenizer->xCreate = &Database::FTSCreate;
@@ -242,7 +243,21 @@ Database::Database(Application *app, QObject *parent, const QString &database_na
}
Database::~Database() {}
Database::~Database() {
QMutexLocker l(&connect_mutex_);
for (QString connection : connections_) {
qLog(Error) << connection << "still open!";
}
if (!connections_.isEmpty())
qLog(Error) << connections_.count() << "connections still open!";
if (sFTSTokenizer)
delete sFTSTokenizer;
}
QSqlDatabase Database::Connect() {
@@ -257,6 +272,11 @@ QSqlDatabase Database::Connect() {
const QString connection_id = QString("%1_thread_%2").arg(connection_id_).arg(reinterpret_cast<quint64>(QThread::currentThread()));
if (!connections_.contains(connection_id)) {
//qLog(Debug) << "Opened database with connection id" << connection_id;
connections_ << connection_id;
}
// Try to find an existing connection for this thread
QSqlDatabase db = QSqlDatabase::database(connection_id);
if (db.isOpen()) {
@@ -346,6 +366,25 @@ QSqlDatabase Database::Connect() {
}
void Database::Close() {
QMutexLocker l(&connect_mutex_);
const QString connection_id = QString("%1_thread_%2").arg(connection_id_).arg(reinterpret_cast<quint64>(QThread::currentThread()));
// Try to find an existing connection for this thread
QSqlDatabase db = QSqlDatabase::database(connection_id);
if (db.isOpen()) {
db.close();
}
if (connections_.contains(connection_id)) {
//qLog(Debug) << "Closed database with connection id" << connection_id;
connections_.removeAll(connection_id);
}
}
void Database::UpdateMainSchema(QSqlDatabase *db) {
// Get the database's schema version