fix: qt client memory leaks (#1378)

* fix: don't leak the qt client's QFileSystemWatcher

* fix: don't leak the Qt client's ListViewProxyStyle
This commit is contained in:
Charles Kerr 2020-07-29 18:21:11 -05:00 committed by GitHub
parent 77ced2b7c8
commit 49400ab443
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 16 deletions

View File

@ -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

View File

@ -9,6 +9,7 @@
#pragma once
#include <ctime>
#include <memory>
#include <QMainWindow>
#include <QNetworkReply>
@ -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<ListViewProxyStyle> lvp_style_;
QPixmap pixmap_network_error_;
QPixmap pixmap_network_idle_;
QPixmap pixmap_network_receive_;

View File

@ -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<QFileSystemWatcher>(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);
}

View File

@ -8,12 +8,13 @@
#pragma once
#include <memory>
#include <QObject>
#include <QFileSystemWatcher>
#include <QSet>
#include <QString>
class QFileSystemWatcher;
class TorrentModel;
class WatchDir : public QObject
@ -49,5 +50,5 @@ private:
TorrentModel const& model_;
QSet<QString> watch_dir_files_;
QFileSystemWatcher* watcher_ = {};
std::unique_ptr<QFileSystemWatcher> watcher_;
};