Skip to content

Commit 501a14b

Browse files
committed
fix: compiler warnings
Mostly QFutureWatcher leaks, fixed by using QFuture.then Also calculator false positive leak
1 parent a034467 commit 501a14b

5 files changed

Lines changed: 26 additions & 55 deletions

File tree

plugin/src/Caelestia/Internal/cachingimagemanager.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,34 +105,26 @@ void CachingImageManager::updateSource(const QString& path) {
105105

106106
m_shaPath = path;
107107

108-
const auto future = QtConcurrent::run(&CachingImageManager::sha256sum, path);
109-
110-
const auto watcher = new QFutureWatcher<QString>(this);
111-
112-
connect(watcher, &QFutureWatcher<QString>::finished, this, [watcher, path, this]() {
108+
QtConcurrent::run(&CachingImageManager::sha256sum, path).then(this, [path, this](const QString& sha) {
113109
if (m_path != path) {
114-
// Object is destroyed or path has changed, ignore
115-
watcher->deleteLater();
116110
return;
117111
}
118112

119113
const QSize size = effectiveSize();
120114

121115
if (!m_item || !size.width() || !size.height()) {
122-
watcher->deleteLater();
123116
return;
124117
}
125118

126119
const QString fillMode = m_item->property("fillMode").toString();
127120
// clang-format off
128121
const QString filename = QString("%1@%2x%3-%4.png")
129-
.arg(watcher->result()).arg(size.width()).arg(size.height())
122+
.arg(sha).arg(size.width()).arg(size.height())
130123
.arg(fillMode == "PreserveAspectCrop" ? "crop" : fillMode == "PreserveAspectFit" ? "fit" : "stretch");
131124
// clang-format on
132125

133126
const QUrl cache = m_cacheDir.resolved(QUrl(filename));
134127
if (m_cachePath == cache) {
135-
watcher->deleteLater();
136128
return;
137129
}
138130

@@ -141,7 +133,6 @@ void CachingImageManager::updateSource(const QString& path) {
141133

142134
if (!cache.isLocalFile()) {
143135
qWarning() << "CachingImageManager::updateSource: cachePath" << cache << "is not a local file";
144-
watcher->deleteLater();
145136
return;
146137
}
147138

@@ -157,11 +148,7 @@ void CachingImageManager::updateSource(const QString& path) {
157148
if (m_shaPath == path) {
158149
m_shaPath = QString();
159150
}
160-
161-
watcher->deleteLater();
162151
});
163-
164-
watcher->setFuture(future);
165152
}
166153

167154
QUrl CachingImageManager::cachePath() const {

plugin/src/Caelestia/Models/filesystemmodel.cpp

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ void FileSystemModel::watchDirIfRecursive(const QString& path) {
219219
if (m_recursive && m_watchChanges) {
220220
const auto currentDir = m_dir;
221221
const bool showHidden = m_showHidden;
222-
const auto future = QtConcurrent::run([showHidden, path]() {
222+
auto future = QtConcurrent::run([showHidden, path]() {
223223
QDir::Filters filters = QDir::Dirs | QDir::NoDotAndDotDot;
224224
if (showHidden) {
225225
filters |= QDir::Hidden;
@@ -232,16 +232,12 @@ void FileSystemModel::watchDirIfRecursive(const QString& path) {
232232
}
233233
return dirs;
234234
});
235-
const auto watcher = new QFutureWatcher<QStringList>(this);
236-
connect(watcher, &QFutureWatcher<QStringList>::finished, this, [currentDir, showHidden, watcher, this]() {
237-
const auto paths = watcher->result();
235+
future.then(this, [currentDir, showHidden, this](const QStringList& paths) {
238236
if (currentDir == m_dir && showHidden == m_showHidden && !paths.isEmpty()) {
239237
// Ignore if dir or showHidden has changed
240238
m_watcher.addPaths(paths);
241239
}
242-
watcher->deleteLater();
243240
});
244-
watcher->setFuture(future);
245241
}
246242
}
247243

@@ -295,7 +291,7 @@ void FileSystemModel::updateEntriesForDir(const QString& dir) {
295291
oldPaths << entry->path();
296292
}
297293

298-
const auto future = QtConcurrent::run([=](QPromise<QPair<QSet<QString>, QSet<QString>>>& promise) {
294+
auto future = QtConcurrent::run([=](QPromise<QPair<QSet<QString>, QSet<QString>>>& promise) {
299295
const auto flags = recursive ? QDirIterator::Subdirectories : QDirIterator::NoIteratorFlags;
300296

301297
std::optional<QDirIterator> iter;
@@ -353,7 +349,7 @@ void FileSystemModel::updateEntriesForDir(const QString& dir) {
353349
newPaths.insert(path);
354350
}
355351

356-
if (promise.isCanceled() || newPaths == oldPaths) {
352+
if (promise.isCanceled()) {
357353
return;
358354
}
359355

@@ -365,23 +361,17 @@ void FileSystemModel::updateEntriesForDir(const QString& dir) {
365361
}
366362
m_futures.insert(dir, future);
367363

368-
const auto watcher = new QFutureWatcher<QPair<QSet<QString>, QSet<QString>>>(this);
369-
370-
connect(watcher, &QFutureWatcher<QPair<QSet<QString>, QSet<QString>>>::finished, this, [dir, watcher, this]() {
371-
m_futures.remove(dir);
372-
373-
if (!watcher->future().isResultReadyAt(0)) {
374-
watcher->deleteLater();
375-
return;
376-
}
377-
378-
const auto result = watcher->result();
379-
applyChanges(result.first, result.second);
380-
381-
watcher->deleteLater();
382-
});
383-
384-
watcher->setFuture(future);
364+
future
365+
.then(this,
366+
[dir, this](QPair<QSet<QString>, QSet<QString>> result) {
367+
m_futures.remove(dir);
368+
if (!result.first.isEmpty() || !result.second.isEmpty()) {
369+
applyChanges(result.first, result.second);
370+
}
371+
})
372+
.onCanceled(this, [dir, this]() {
373+
m_futures.remove(dir);
374+
});
385375
}
386376

387377
void FileSystemModel::applyChanges(const QSet<QString>& removedPaths, const QSet<QString>& addedPaths) {

plugin/src/Caelestia/Services/audiocollector.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ AudioCollector::AudioCollector(QObject* parent)
221221
, m_writeBuffer(&m_buffer2) {}
222222

223223
AudioCollector::~AudioCollector() {
224-
stop();
224+
AudioCollector::stop();
225225
}
226226

227227
void AudioCollector::start() {

plugin/src/Caelestia/appdb.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ AppEntry::AppEntry(QObject* entry, unsigned int frequency, QObject* parent)
1111
, m_entry(entry)
1212
, m_frequency(frequency) {
1313
const auto mo = m_entry->metaObject();
14-
const auto tmo = metaObject();
14+
const auto tmo = &AppEntry::staticMetaObject;
1515

1616
for (const auto& prop :
1717
{ "name", "comment", "execString", "startupClass", "genericName", "categories", "keywords" }) {

plugin/src/Caelestia/qalculator.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "qalculator.hpp"
22

33
#include <libqalculate/qalculate.h>
4-
#include <qfuturewatcher.h>
54
#include <qtconcurrentrun.h>
65

76
namespace caelestia {
@@ -11,7 +10,10 @@ QMutex Qalculator::s_calculatorMutex;
1110
Qalculator::Qalculator(QObject* parent)
1211
: QObject(parent) {
1312
if (!CALCULATOR) {
14-
new Calculator();
13+
// Calculator constructor sets the global `calculator` pointer (CALCULATOR macro),
14+
// but we need to assign it to a var so compiler doesn't flag it as a leak
15+
static const auto* const instance = new Calculator();
16+
Q_UNUSED(instance)
1517
CALCULATOR->loadExchangeRates();
1618
CALCULATOR->loadGlobalDefinitions();
1719
CALCULATOR->loadLocalDefinitions();
@@ -79,7 +81,7 @@ void Qalculator::evalAsync(const QString& expr) {
7981
emit busyChanged();
8082
}
8183

82-
const auto future = QtConcurrent::run([expr]() -> QPair<QString, QString> {
84+
QtConcurrent::run([expr]() -> QPair<QString, QString> {
8385
QMutexLocker locker(&s_calculatorMutex);
8486

8587
EvaluationOptions eo;
@@ -109,18 +111,12 @@ void Qalculator::evalAsync(const QString& expr) {
109111

110112
const QString rawStr = QString::fromStdString(result);
111113
return { QString("%1 = %2").arg(parsed).arg(result), rawStr };
112-
});
113-
114-
auto* watcher = new QFutureWatcher<QPair<QString, QString>>(this);
115-
116-
connect(watcher, &QFutureWatcher<QPair<QString, QString>>::finished, this, [this, watcher, gen]() {
117-
watcher->deleteLater();
118-
114+
}).then(this, [this, gen](QPair<QString, QString> result) {
119115
if (gen != m_generation) {
120116
return;
121117
}
122118

123-
const auto [formatted, raw] = watcher->result();
119+
const auto& [formatted, raw] = result;
124120

125121
if (m_result != formatted) {
126122
m_result = formatted;
@@ -135,8 +131,6 @@ void Qalculator::evalAsync(const QString& expr) {
135131
emit busyChanged();
136132
}
137133
});
138-
139-
watcher->setFuture(future);
140134
}
141135

142136
QString Qalculator::result() const {

0 commit comments

Comments
 (0)