From fa9ab74ffa85a94eb215b5c5e689fac324919d30 Mon Sep 17 00:00:00 2001 From: Guillaume Munch Date: Sat, 18 Mar 2017 20:37:55 +0100 Subject: [PATCH] Correct ownership One can now delete a file monitor if a blocker is active. --- src/support/FileMonitor.cpp | 17 ++++++++--------- src/support/FileMonitor.h | 5 +++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/support/FileMonitor.cpp b/src/support/FileMonitor.cpp index bf53f379ed..c14d3d6e3a 100644 --- a/src/support/FileMonitor.cpp +++ b/src/support/FileMonitor.cpp @@ -31,7 +31,7 @@ namespace support { FileSystemWatcher & FileSystemWatcher::instance() { - // This thread-safe because QFileSystemWatcher is thread-safe. + // This is thread-safe because QFileSystemWatcher is thread-safe. static FileSystemWatcher f; return f; } @@ -190,11 +190,11 @@ FileMonitorBlocker FileMonitor::block(int delay) } -FileMonitorBlockerGuard::FileMonitorBlockerGuard(FileMonitor * parent) - : QObject(parent), parent_(parent), delay_(0) +FileMonitorBlockerGuard::FileMonitorBlockerGuard(FileMonitor * monitor) + : monitor_(monitor), delay_(0) { - QObject::disconnect(parent_->monitor_.get(), SIGNAL(fileChanged()), - parent_, SLOT(changed())); + QObject::disconnect(monitor->monitor_.get(), SIGNAL(fileChanged()), + monitor, SLOT(changed())); } @@ -206,14 +206,13 @@ void FileMonitorBlockerGuard::setDelay(int delay) FileMonitorBlockerGuard::~FileMonitorBlockerGuard() { - // closures can only copy local copies - FileMonitor * parent = parent_; - // parent is also our QObject::parent() so we are deleted before parent. + if (!monitor_) + return; // Even if delay_ is 0, the QTimer is necessary. Indeed, the notifications // from QFileSystemWatcher that we meant to ignore are not going to be // treated immediately, so we must yield to give us the opportunity to // ignore them. - QTimer::singleShot(delay_, parent, SLOT(connectToFileMonitorGuard())); + QTimer::singleShot(delay_, monitor_, SLOT(reconnectToFileMonitorGuard())); } } // namespace support diff --git a/src/support/FileMonitor.h b/src/support/FileMonitor.h index deca740657..768e61f873 100644 --- a/src/support/FileMonitor.h +++ b/src/support/FileMonitor.h @@ -19,6 +19,7 @@ #include #include +#include #include @@ -132,11 +133,11 @@ private: class FileMonitorBlockerGuard : public QObject { Q_OBJECT - FileMonitor * parent_; + QPointer monitor_; int delay_; public: - FileMonitorBlockerGuard(FileMonitor * parent); + FileMonitorBlockerGuard(FileMonitor * monitor); ~FileMonitorBlockerGuard(); void setDelay(int delay); }; -- 2.39.2