Safely close database connections and delete backends
Also fix NewClosure leak caused by disconnected object signals
This commit is contained in:
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ class DeviceDatabaseBackend : public QObject {
|
||||
|
||||
public:
|
||||
Q_INVOKABLE DeviceDatabaseBackend(QObject *parent = nullptr);
|
||||
~DeviceDatabaseBackend();
|
||||
|
||||
struct Device {
|
||||
Device() : id_(-1) {}
|
||||
@@ -58,6 +59,9 @@ class DeviceDatabaseBackend : public QObject {
|
||||
static const int kDeviceSchemaVersion;
|
||||
|
||||
void Init(Database *db);
|
||||
void Close();
|
||||
void ExitAsync();
|
||||
|
||||
Database *db() const { return db_; }
|
||||
|
||||
DeviceList GetAllDevices();
|
||||
@@ -66,8 +70,16 @@ class DeviceDatabaseBackend : public QObject {
|
||||
|
||||
void SetDeviceOptions(int id, const QString &friendly_name, const QString &icon_name, MusicStorage::TranscodeMode mode, Song::FileType format);
|
||||
|
||||
private slots:
|
||||
void Exit();
|
||||
|
||||
signals:
|
||||
void ExitFinished();
|
||||
|
||||
private:
|
||||
Database *db_;
|
||||
QThread *original_thread_;
|
||||
|
||||
};
|
||||
|
||||
#endif // DEVICEDATABASEBACKEND_H
|
||||
|
||||
@@ -158,9 +158,17 @@ DeviceManager::~DeviceManager() {
|
||||
delete lister;
|
||||
}
|
||||
|
||||
backend_->deleteLater();
|
||||
backend_->Close();
|
||||
|
||||
delete root_;
|
||||
delete backend_;
|
||||
|
||||
}
|
||||
|
||||
void DeviceManager::Exit() {
|
||||
|
||||
connect(backend_, SIGNAL(ExitFinished()), this, SIGNAL(ExitFinished()));
|
||||
backend_->ExitAsync();
|
||||
|
||||
}
|
||||
|
||||
@@ -174,6 +182,7 @@ void DeviceManager::LoadAllDevices() {
|
||||
info->InitFromDb(device);
|
||||
emit DeviceCreatedFromDB(info);
|
||||
}
|
||||
backend_->Close();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -85,6 +85,8 @@ class DeviceManager : public SimpleTreeModel<DeviceInfo> {
|
||||
static const int kDeviceIconSize;
|
||||
static const int kDeviceIconOverlaySize;
|
||||
|
||||
void Exit();
|
||||
|
||||
DeviceStateFilterModel *connected_devices_model() const { return connected_devices_model_; }
|
||||
|
||||
// Get info about devices
|
||||
@@ -115,6 +117,7 @@ class DeviceManager : public SimpleTreeModel<DeviceInfo> {
|
||||
void Unmount(QModelIndex idx);
|
||||
|
||||
signals:
|
||||
void ExitFinished();
|
||||
void DeviceConnected(QModelIndex idx);
|
||||
void DeviceDisconnected(QModelIndex idx);
|
||||
void DeviceCreatedFromDB(DeviceInfo* info);
|
||||
|
||||
@@ -102,6 +102,8 @@ Itdb_iTunesDB *GPodLoader::TryLoad() {
|
||||
// Add the songs we've just loaded
|
||||
backend_->AddOrUpdateSongs(songs);
|
||||
|
||||
backend_->Close();
|
||||
|
||||
return db;
|
||||
|
||||
}
|
||||
|
||||
@@ -96,6 +96,8 @@ bool MtpLoader::TryLoad() {
|
||||
// Add the songs we've just loaded
|
||||
backend_->AddOrUpdateSongs(songs);
|
||||
|
||||
backend_->Close();
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user