Track request lifetime in lambda callbacks

This commit is contained in:
Atlante45 2016-05-12 13:49:43 -07:00
parent 0878d87ac7
commit 419ff3bbea
2 changed files with 12 additions and 6 deletions

View file

@ -202,7 +202,7 @@ AssetUpload* AssetClient::createUpload(const QByteArray& data) {
}
MessageID AssetClient::getAsset(const QString& hash, DataOffset start, DataOffset end,
ReceivedAssetCallback callback, ProgressCallback progressCallback) {
ReceivedAssetCallback callback, ProgressCallback progressCallback) {
Q_ASSERT(QThread::currentThread() == thread());
if (hash.length() != SHA256_HASH_HEX_LENGTH) {
@ -238,8 +238,6 @@ MessageID AssetClient::getAsset(const QString& hash, DataOffset start, DataOffse
callback(false, AssetServerError::NoError, QByteArray());
return INVALID_MESSAGE_ID;
}
}
MessageID AssetClient::getAssetInfo(const QString& hash, GetInfoCallback callback) {

View file

@ -106,9 +106,13 @@ void AssetRequest::start() {
int start = 0, end = _info.size;
auto assetClient = DependencyManager::get<AssetClient>();
auto that = QPointer<AssetRequest>(this); // Used to track the request's lifetime
_assetRequestID = assetClient->getAsset(_hash, start, end,
[this, start, end](bool responseReceived, AssetServerError serverError, const QByteArray& data) {
[this, that, start, end](bool responseReceived, AssetServerError serverError, const QByteArray& data) {
if (!that) {
// If the request is dead, return
return;
}
_assetRequestID = AssetClient::INVALID_MESSAGE_ID;
if (!responseReceived) {
@ -148,7 +152,11 @@ void AssetRequest::start() {
_state = Finished;
emit finished(this);
}, [this](qint64 totalReceived, qint64 total) {
}, [this, that](qint64 totalReceived, qint64 total) {
if (!that) {
// If the request is dead, return
return;
}
emit progress(totalReceived, total);
});
});