From 49400ab443ecf9350af976700926573f5ce6c0a6 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 29 Jul 2020 18:21:11 -0500 Subject: [PATCH] fix: qt client memory leaks (#1378) * fix: don't leak the qt client's QFileSystemWatcher * fix: don't leak the Qt client's ListViewProxyStyle --- qt/MainWindow.cc | 3 ++- qt/MainWindow.h | 4 ++++ qt/WatchDir.cc | 17 +++++------------ qt/WatchDir.h | 7 ++++--- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/qt/MainWindow.cc b/qt/MainWindow.cc index e41d3b873..a788d62b1 100644 --- a/qt/MainWindow.cc +++ b/qt/MainWindow.cc @@ -142,6 +142,7 @@ MainWindow::MainWindow(Session& session, Prefs& prefs, TorrentModel& model, bool session_(session), prefs_(prefs), model_(model), + lvp_style_(new ListViewProxyStyle{}), filter_model_(prefs), torrent_delegate_(new TorrentDelegate(this)), torrent_delegate_min_(new TorrentDelegateMin(this)), @@ -155,7 +156,7 @@ MainWindow::MainWindow(Session& session, Prefs& prefs, TorrentModel& model, bool ui_.setupUi(this); - ui_.listView->setStyle(new ListViewProxyStyle); + ui_.listView->setStyle(lvp_style_.get()); ui_.listView->setAttribute(Qt::WA_MacShowFocusRect, false); // icons diff --git a/qt/MainWindow.h b/qt/MainWindow.h index c2ae44d4c..92fba3845 100644 --- a/qt/MainWindow.h +++ b/qt/MainWindow.h @@ -9,6 +9,7 @@ #pragma once #include +#include #include #include @@ -32,6 +33,7 @@ class QStringList; class AboutDialog; class AddData; class DetailsDialog; +class ListViewProxyStyle; class Prefs; class PrefsDialog; class Session; @@ -142,6 +144,8 @@ private: Prefs& prefs_; TorrentModel& model_; + std::shared_ptr lvp_style_; + QPixmap pixmap_network_error_; QPixmap pixmap_network_idle_; QPixmap pixmap_network_receive_; diff --git a/qt/WatchDir.cc b/qt/WatchDir.cc index db96b00e1..4da67d96c 100644 --- a/qt/WatchDir.cc +++ b/qt/WatchDir.cc @@ -81,20 +81,13 @@ void WatchDir::setPath(QString const& path, bool is_enabled) { // clear out any remnants of the previous watcher, if any watch_dir_files_.clear(); - - if (watcher_ != nullptr) - { - delete watcher_; - watcher_ = nullptr; - } + watcher_.reset(); // maybe create a new watcher if (is_enabled) { - watcher_ = new QFileSystemWatcher(); - watcher_->addPath(path); - connect(watcher_, SIGNAL(directoryChanged(QString)), this, SLOT(watcherActivated(QString))); - // std::cerr << "watching " << qPrintable(path) << " for new .torrent files" << std::endl; + watcher_ = std::make_unique(QStringList{ path }); + connect(watcher_.get(), SIGNAL(directoryChanged(QString)), this, SLOT(watcherActivated(QString))); QTimer::singleShot(0, this, SLOT(rescanAllWatchedDirectories())); // trigger the watchdir for .torrent files in there already } } @@ -150,12 +143,12 @@ void WatchDir::watcherActivated(QString const& path) void WatchDir::rescanAllWatchedDirectories() { - if (watcher_ == nullptr) + if (!watcher_) { return; } - for (QString const& path : watcher_->directories()) + for (auto const& path : watcher_->directories()) { watcherActivated(path); } diff --git a/qt/WatchDir.h b/qt/WatchDir.h index 41b50cc9c..7542922df 100644 --- a/qt/WatchDir.h +++ b/qt/WatchDir.h @@ -8,12 +8,13 @@ #pragma once +#include + #include +#include #include #include -class QFileSystemWatcher; - class TorrentModel; class WatchDir : public QObject @@ -49,5 +50,5 @@ private: TorrentModel const& model_; QSet watch_dir_files_; - QFileSystemWatcher* watcher_ = {}; + std::unique_ptr watcher_; };