From 090a4b5ff8f6b31a4eea4bdb75eb1350aa3efae0 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 24 Jan 2021 22:20:21 -0600 Subject: [PATCH] fix: dangling reference crash in FreeSpaceLabel (#1604) --- qt/FreeSpaceLabel.cc | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/qt/FreeSpaceLabel.cc b/qt/FreeSpaceLabel.cc index 143575330..471681084 100644 --- a/qt/FreeSpaceLabel.cc +++ b/qt/FreeSpaceLabel.cc @@ -73,29 +73,37 @@ void FreeSpaceLabel::onTimer() auto* q = new RpcQueue(); + auto* alive = new bool(true); + connect(this, &QObject::destroyed, [alive] { *alive = false; }); + q->add([this, &args]() { return session_->exec("free-space", &args); }); - q->add([this](RpcResponse const& r) + q->add([this, alive](RpcResponse const& r) { - // update the label - auto const bytes = dictFind(r.args.get(), TR_KEY_size_bytes); - if (bytes && *bytes > 1) + if (*alive) { - setText(tr("%1 free").arg(Formatter::get().sizeToString(*bytes))); - } - else - { - setText(QString()); + // update the label + auto const bytes = dictFind(r.args.get(), TR_KEY_size_bytes); + if (bytes && *bytes > 1) + { + setText(tr("%1 free").arg(Formatter::get().sizeToString(*bytes))); + } + else + { + clear(); + } + + // update the tooltip + auto const path = dictFind(r.args.get(), TR_KEY_path); + setToolTip(QDir::toNativeSeparators(path ? *path : QString())); + + timer_.start(); } - // update the tooltip - auto const path = dictFind(r.args.get(), TR_KEY_path); - setToolTip(QDir::toNativeSeparators(path ? *path : QString())); - - timer_.start(); + delete alive; }); q->run();