Fix async result handlers

This commit is contained in:
Vladimir Golovnev (Glassez) 2025-05-08 07:51:18 +03:00
commit 9065ccdee0
No known key found for this signature in database
GPG key ID: 52A2C7DEE2DFA6F7
4 changed files with 21 additions and 19 deletions

View file

@ -409,8 +409,11 @@ void PeerListWidget::loadPeers(const BitTorrent::Torrent *torrent)
using TorrentPtr = QPointer<const BitTorrent::Torrent>; using TorrentPtr = QPointer<const BitTorrent::Torrent>;
torrent->fetchPeerInfo().then(this, [this, torrent = TorrentPtr(torrent)](const QList<BitTorrent::PeerInfo> &peers) torrent->fetchPeerInfo().then(this, [this, torrent = TorrentPtr(torrent)](const QList<BitTorrent::PeerInfo> &peers)
{ {
if (torrent != m_properties->getCurrentTorrent()) if (const BitTorrent::Torrent *currentTorrent = m_properties->getCurrentTorrent();
!currentTorrent || (currentTorrent != torrent))
{
return; return;
}
// Remove I2P peers since they will be completely reloaded. // Remove I2P peers since they will be completely reloaded.
for (const QStandardItem *item : asConst(m_I2PPeerItems)) for (const QStandardItem *item : asConst(m_I2PPeerItems))

View file

@ -480,9 +480,10 @@ void PropertiesWidget::loadDynamicData()
showPiecesAvailability(true); showPiecesAvailability(true);
using TorrentPtr = QPointer<BitTorrent::Torrent>; using TorrentPtr = QPointer<BitTorrent::Torrent>;
m_torrent->fetchPieceAvailability().then(this, [this, torrent = TorrentPtr(m_torrent)](const QList<int> &pieceAvailability) m_torrent->fetchPieceAvailability().then(this
, [this, torrent = TorrentPtr(m_torrent)](const QList<int> &pieceAvailability)
{ {
if (torrent == m_torrent) if (m_torrent && (m_torrent == torrent))
m_piecesAvailability->setAvailability(pieceAvailability); m_piecesAvailability->setAvailability(pieceAvailability);
}); });
@ -497,9 +498,12 @@ void PropertiesWidget::loadDynamicData()
qreal progress = m_torrent->progress() * 100.; qreal progress = m_torrent->progress() * 100.;
m_ui->labelProgressVal->setText(Utils::String::fromDouble(progress, 1) + u'%'); m_ui->labelProgressVal->setText(Utils::String::fromDouble(progress, 1) + u'%');
m_torrent->fetchDownloadingPieces().then(this, [this](const QBitArray &downloadingPieces) using TorrentPtr = QPointer<BitTorrent::Torrent>;
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 else
@ -527,7 +531,7 @@ void PropertiesWidget::loadUrlSeeds()
using TorrentPtr = QPointer<BitTorrent::Torrent>; using TorrentPtr = QPointer<BitTorrent::Torrent>;
m_torrent->fetchURLSeeds().then(this, [this, torrent = TorrentPtr(m_torrent)](const QList<QUrl> &urlSeeds) m_torrent->fetchURLSeeds().then(this, [this, torrent = TorrentPtr(m_torrent)](const QList<QUrl> &urlSeeds)
{ {
if (torrent != m_torrent) if (!m_torrent || (m_torrent != torrent))
return; return;
m_ui->listWebSeeds->clear(); m_ui->listWebSeeds->clear();

View file

@ -223,16 +223,11 @@ void TorrentContentModel::updateFilesAvailability()
m_contentHandler->fetchAvailableFileFractions().then(this m_contentHandler->fetchAvailableFileFractions().then(this
, [this, handler = HandlerPtr(m_contentHandler)](const QList<qreal> &availableFileFractions) , [this, handler = HandlerPtr(m_contentHandler)](const QList<qreal> &availableFileFractions)
{ {
if (handler != m_contentHandler) if (!m_contentHandler || (m_contentHandler != handler))
return;
Q_ASSERT(m_filesIndex.size() == availableFileFractions.size());
// XXX: Why is this necessary?
if (m_filesIndex.size() != availableFileFractions.size()) [[unlikely]]
return; return;
for (int i = 0; i < m_filesIndex.size(); ++i) 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 // Update folders progress in the tree
m_rootItem->recalculateProgress(); m_rootItem->recalculateProgress();
}); });

View file

@ -312,14 +312,14 @@ void TrackerListModel::populate()
using TorrentPtr = QPointer<const BitTorrent::Torrent>; using TorrentPtr = QPointer<const BitTorrent::Torrent>;
m_torrent->fetchPeerInfo().then(this, [this, torrent = TorrentPtr(m_torrent)](const QList<BitTorrent::PeerInfo> &peers) m_torrent->fetchPeerInfo().then(this, [this, torrent = TorrentPtr(m_torrent)](const QList<BitTorrent::PeerInfo> &peers)
{ {
if (torrent != m_torrent) if (!m_torrent || (m_torrent != torrent))
return; return;
// XXX: libtorrent should provide this info... // XXX: libtorrent should provide this info...
// Count peers from DHT, PeX, LSD // Count peers from DHT, PeX, LSD
uint seedsDHT = 0, seedsPeX = 0, seedsLSD = 0, peersDHT = 0, peersPeX = 0, peersLSD = 0; uint seedsDHT = 0, seedsPeX = 0, seedsLSD = 0, peersDHT = 0, peersPeX = 0, peersLSD = 0;
for (const BitTorrent::PeerInfo &peer : peers) for (const BitTorrent::PeerInfo &peer : peers)
{ {
if (peer.isConnecting()) if (peer.isConnecting())
continue; continue;