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

@@ -18,9 +18,14 @@
*
*/
#include "config.h"
#include <stdbool.h>
#include <assert.h>
#include <QObject>
#include <QApplication>
#include <QThread>
#include <QMutex>
#include <QIODevice>
#include <QFile>
@@ -32,17 +37,47 @@
#include "core/database.h"
#include "core/scopedtransaction.h"
#include "core/logging.h"
#include "devicedatabasebackend.h"
const int DeviceDatabaseBackend::kDeviceSchemaVersion = 0;
DeviceDatabaseBackend::DeviceDatabaseBackend(QObject *parent) :
QObject(parent),
db_(nullptr)
{}
db_(nullptr),
original_thread_(nullptr)
{
original_thread_ = thread();
}
DeviceDatabaseBackend::~DeviceDatabaseBackend() {}
void DeviceDatabaseBackend::Init(Database* db) { db_ = db; }
void DeviceDatabaseBackend::Close() {
if (db_) {
QMutexLocker l(db_->Mutex());
db_->Close();
}
}
void DeviceDatabaseBackend::ExitAsync() {
metaObject()->invokeMethod(this, "Exit", Qt::QueuedConnection);
}
void DeviceDatabaseBackend::Exit() {
assert(QThread::currentThread() == thread());
Close();
moveToThread(original_thread_);
emit ExitFinished();
}
DeviceDatabaseBackend::DeviceList DeviceDatabaseBackend::GetAllDevices() {
QMutexLocker l(db_->Mutex());
@@ -101,6 +136,7 @@ int DeviceDatabaseBackend::AddDevice(const Device &device) {
db_->ExecSchemaCommands(db, schema, 0, true);
t.Commit();
return id;
}