From 9065ccdee01d87374e78493bff8c0a44ee08ecaa Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Thu, 8 May 2025 07:51:18 +0300 Subject: [PATCH] Fix async result handlers --- src/gui/properties/peerlistwidget.cpp | 5 ++++- src/gui/properties/propertieswidget.cpp | 14 +++++++++----- src/gui/torrentcontentmodel.cpp | 9 ++------- src/gui/trackerlist/trackerlistmodel.cpp | 12 ++++++------ 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/gui/properties/peerlistwidget.cpp b/src/gui/properties/peerlistwidget.cpp index a6c44d106..4c27d4eb4 100644 --- a/src/gui/properties/peerlistwidget.cpp +++ b/src/gui/properties/peerlistwidget.cpp @@ -409,8 +409,11 @@ void PeerListWidget::loadPeers(const BitTorrent::Torrent *torrent) using TorrentPtr = QPointer; torrent->fetchPeerInfo().then(this, [this, torrent = TorrentPtr(torrent)](const QList &peers) { - if (torrent != m_properties->getCurrentTorrent()) + if (const BitTorrent::Torrent *currentTorrent = m_properties->getCurrentTorrent(); + !currentTorrent || (currentTorrent != torrent)) + { return; + } // Remove I2P peers since they will be completely reloaded. for (const QStandardItem *item : asConst(m_I2PPeerItems)) diff --git a/src/gui/properties/propertieswidget.cpp b/src/gui/properties/propertieswidget.cpp index 8a76e4663..d86f5a074 100644 --- a/src/gui/properties/propertieswidget.cpp +++ b/src/gui/properties/propertieswidget.cpp @@ -480,9 +480,10 @@ void PropertiesWidget::loadDynamicData() showPiecesAvailability(true); using TorrentPtr = QPointer; - m_torrent->fetchPieceAvailability().then(this, [this, torrent = TorrentPtr(m_torrent)](const QList &pieceAvailability) + m_torrent->fetchPieceAvailability().then(this + , [this, torrent = TorrentPtr(m_torrent)](const QList &pieceAvailability) { - if (torrent == m_torrent) + if (m_torrent && (m_torrent == torrent)) m_piecesAvailability->setAvailability(pieceAvailability); }); @@ -497,9 +498,12 @@ void PropertiesWidget::loadDynamicData() qreal progress = m_torrent->progress() * 100.; m_ui->labelProgressVal->setText(Utils::String::fromDouble(progress, 1) + u'%'); - m_torrent->fetchDownloadingPieces().then(this, [this](const QBitArray &downloadingPieces) + using TorrentPtr = QPointer; + m_torrent->fetchDownloadingPieces().then(this + , [this, torrent = TorrentPtr(m_torrent)](const QBitArray &downloadingPieces) { - m_downloadedPieces->setProgress(m_torrent->pieces(), downloadingPieces); + if (m_torrent && (m_torrent == torrent)) + m_downloadedPieces->setProgress(m_torrent->pieces(), downloadingPieces); }); } else @@ -527,7 +531,7 @@ void PropertiesWidget::loadUrlSeeds() using TorrentPtr = QPointer; m_torrent->fetchURLSeeds().then(this, [this, torrent = TorrentPtr(m_torrent)](const QList &urlSeeds) { - if (torrent != m_torrent) + if (!m_torrent || (m_torrent != torrent)) return; m_ui->listWebSeeds->clear(); diff --git a/src/gui/torrentcontentmodel.cpp b/src/gui/torrentcontentmodel.cpp index 32e44c16f..2df7b8176 100644 --- a/src/gui/torrentcontentmodel.cpp +++ b/src/gui/torrentcontentmodel.cpp @@ -223,16 +223,11 @@ void TorrentContentModel::updateFilesAvailability() m_contentHandler->fetchAvailableFileFractions().then(this , [this, handler = HandlerPtr(m_contentHandler)](const QList &availableFileFractions) { - if (handler != m_contentHandler) - return; - - Q_ASSERT(m_filesIndex.size() == availableFileFractions.size()); - // XXX: Why is this necessary? - if (m_filesIndex.size() != availableFileFractions.size()) [[unlikely]] + if (!m_contentHandler || (m_contentHandler != handler)) return; for (int i = 0; i < m_filesIndex.size(); ++i) - m_filesIndex[i]->setAvailability(availableFileFractions[i]); + m_filesIndex[i]->setAvailability(availableFileFractions.value(i, 0)); // Update folders progress in the tree m_rootItem->recalculateProgress(); }); diff --git a/src/gui/trackerlist/trackerlistmodel.cpp b/src/gui/trackerlist/trackerlistmodel.cpp index b13b067b6..4c90e0a6a 100644 --- a/src/gui/trackerlist/trackerlistmodel.cpp +++ b/src/gui/trackerlist/trackerlistmodel.cpp @@ -312,14 +312,14 @@ void TrackerListModel::populate() using TorrentPtr = QPointer; m_torrent->fetchPeerInfo().then(this, [this, torrent = TorrentPtr(m_torrent)](const QList &peers) { - if (torrent != m_torrent) + if (!m_torrent || (m_torrent != torrent)) return; - // XXX: libtorrent should provide this info... - // Count peers from DHT, PeX, LSD - uint seedsDHT = 0, seedsPeX = 0, seedsLSD = 0, peersDHT = 0, peersPeX = 0, peersLSD = 0; - for (const BitTorrent::PeerInfo &peer : peers) - { + // XXX: libtorrent should provide this info... + // Count peers from DHT, PeX, LSD + uint seedsDHT = 0, seedsPeX = 0, seedsLSD = 0, peersDHT = 0, peersPeX = 0, peersLSD = 0; + for (const BitTorrent::PeerInfo &peer : peers) + { if (peer.isConnecting()) continue;