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

@@ -21,6 +21,7 @@
#include "config.h"
#include <stdbool.h>
#include <unistd.h>
#include <QObject>
#include <QThread>
@@ -50,7 +51,10 @@ SCollection::SCollection(Application *app, QObject *parent)
backend_(nullptr),
model_(nullptr),
watcher_(nullptr),
watcher_thread_(nullptr) {
watcher_thread_(nullptr),
original_thread_(nullptr) {
original_thread_ = thread();
backend_ = new CollectionBackend();
backend()->moveToThread(app->database()->thread());
@@ -64,11 +68,17 @@ SCollection::SCollection(Application *app, QObject *parent)
}
SCollection::~SCollection() {
watcher_->Stop();
watcher_->deleteLater();
watcher_thread_->exit();
watcher_thread_->wait(5000 /* five seconds */);
if (watcher_) {
watcher_->Stop();
watcher_->deleteLater();
}
if (watcher_thread_) {
watcher_thread_->exit();
watcher_thread_->wait(5000 /* five seconds */);
}
backend_->deleteLater();
}
void SCollection::Init() {
@@ -98,6 +108,29 @@ void SCollection::Init() {
// This will start the watcher checking for updates
backend_->LoadDirectoriesAsync();
}
void SCollection::Exit() {
wait_for_exit_ << backend_ << watcher_;
disconnect(backend_, 0, watcher_, 0);
disconnect(watcher_, 0, backend_, 0);
connect(backend_, SIGNAL(ExitFinished()), this, SLOT(ExitReceived()));
connect(watcher_, SIGNAL(ExitFinished()), this, SLOT(ExitReceived()));
backend_->ExitAsync();
watcher_->ExitAsync();
}
void SCollection::ExitReceived() {
disconnect(sender(), 0, this, 0);
wait_for_exit_.removeAll(sender());
if (wait_for_exit_.isEmpty()) emit ExitFinished();
}
void SCollection::IncrementalScan() { watcher_->IncrementalScanAsync(); }