Ref T730, use Pointer<QEventLoop> loop to avoid issue with deleted references

This commit is contained in:
Klaus Basan
2019-10-06 00:06:09 +02:00
committed by Mat Sutcliffe
parent 9ddb55e79f
commit e30b690191
2 changed files with 29 additions and 29 deletions

View File

@@ -11,7 +11,6 @@
#include "blackmisc/network/networkutils.h" #include "blackmisc/network/networkutils.h"
#include "blackmisc/network/external/qjsonwebtoken.h" #include "blackmisc/network/external/qjsonwebtoken.h"
#include "blackmisc/logmessage.h" #include "blackmisc/logmessage.h"
#include "blackmisc/logcategory.h"
#include <QJsonObject> #include <QJsonObject>
#include <QJsonArray> #include <QJsonArray>
@@ -19,6 +18,7 @@
#include <QUrlQuery> #include <QUrlQuery>
#include <QScopedPointer> #include <QScopedPointer>
#include <QMetaEnum> #include <QMetaEnum>
#include <QPointer>
using namespace BlackMisc; using namespace BlackMisc;
using namespace BlackMisc::Network; using namespace BlackMisc::Network;
@@ -61,23 +61,24 @@ namespace BlackCore
{"networkversion", networkVersion.toString()}, {"networkversion", networkVersion.toString()},
}; };
QPointer<QEventLoop> loop(new QEventLoop(sApp));
QNetworkRequest request(url); QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
QEventLoop loop;
// posted in QAM thread, reply is nullptr if called from another thread // posted in QAM thread, reply is nullptr if called from another thread
QNetworkReply *reply = sApp->postToNetwork(request, CApplication::NoLogRequestId, QJsonDocument(obj).toJson(), QNetworkReply *reply = sApp->postToNetwork(request, CApplication::NoLogRequestId, QJsonDocument(obj).toJson(),
{ {
this, [ & ](QNetworkReply * nwReply) this, [ = ](QNetworkReply * nwReply)
{ {
// called in "this" thread // called in "this" thread
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> reply(nwReply); const QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> reply(nwReply);
if (isShuttingDown()) { return; } if (!loop || isShuttingDown()) { return; }
this->logRequestDuration(reply.data(), "authentication"); this->logRequestDuration(reply.data(), "authentication");
if (reply->error() != QNetworkReply::NoError) if (reply->error() != QNetworkReply::NoError)
{ {
this->logReplyErrorMessage(reply.data(), "authentication error"); this->logReplyErrorMessage(reply.data(), "authentication error");
loop.exit(); if (loop) { loop->exit(); }
return; return;
} }
@@ -91,7 +92,7 @@ namespace BlackCore
do do
{ {
const QString jwtToken(m_jwt); const QString jwtToken(m_jwt);
QJsonWebToken token = QJsonWebToken::fromTokenAndSecret(jwtToken, ""); const QJsonWebToken token = QJsonWebToken::fromTokenAndSecret(jwtToken, "");
// get decoded header and payload // get decoded header and payload
// QString strHeader = token.getHeaderQStr(); // QString strHeader = token.getHeaderQStr();
@@ -115,12 +116,12 @@ namespace BlackCore
m_isAuthenticated = true; m_isAuthenticated = true;
} }
loop.exit(); if (loop) { loop->exit(); }
} }
}); });
Q_UNUSED(reply) Q_UNUSED(reply)
loop.exec(); if (loop) { loop->exec(); }
return m_isAuthenticated; return m_isAuthenticated;
} }
@@ -160,19 +161,19 @@ namespace BlackCore
{ {
if (isShuttingDown()) { return {}; } if (isShuttingDown()) { return {}; }
QEventLoop loop; QPointer<QEventLoop> loop(new QEventLoop(sApp));
QByteArray receivedData; QByteArray receivedData;
// posted in QAM thread, reply is nullptr if called from another thread // posted in QAM thread, reply is nullptr if called from another thread
QNetworkReply *reply = sApp->getFromNetwork(request, QNetworkReply *reply = sApp->getFromNetwork(request,
{ {
this, [ & ](QNetworkReply * nwReply) this, [ =, &receivedData ](QNetworkReply * nwReply)
{ {
const QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> reply(nwReply);
// called in "this" thread // called in "this" thread
if (!isShuttingDown()) if (loop && !isShuttingDown())
{ {
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> reply(nwReply);
if (isShuttingDown()) { return; }
this->logRequestDuration(reply.data()); this->logRequestDuration(reply.data());
if (reply->error() == QNetworkReply::NoError) if (reply->error() == QNetworkReply::NoError)
{ {
@@ -183,12 +184,12 @@ namespace BlackCore
this->logReplyErrorMessage(reply.data()); this->logReplyErrorMessage(reply.data());
} }
} }
loop.exit(); if (loop) { loop->exit(); }
} }
}); });
Q_UNUSED(reply) Q_UNUSED(reply)
loop.exec(); if (loop) { loop->exec(); }
return receivedData; return receivedData;
} }
@@ -196,19 +197,19 @@ namespace BlackCore
{ {
if (isShuttingDown()) { return {}; } if (isShuttingDown()) { return {}; }
QEventLoop loop; QPointer<QEventLoop> loop(new QEventLoop(sApp));
QByteArray receivedData; QByteArray receivedData;
// posted in QAM thread, reply is nullptr if called from another thread // posted in QAM thread, reply is nullptr if called from another thread
QNetworkReply *reply = sApp->postToNetwork(request, CApplication::NoLogRequestId, data, QNetworkReply *reply = sApp->postToNetwork(request, CApplication::NoLogRequestId, data,
{ {
this, [ & ](QNetworkReply * nwReply) this, [ =, &receivedData ](QNetworkReply * nwReply)
{ {
const QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> reply(nwReply);
// called in "this" thread // called in "this" thread
if (!isShuttingDown()) if (loop && !isShuttingDown())
{ {
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> reply(nwReply);
if (isShuttingDown()) { return; }
this->logRequestDuration(reply.data()); this->logRequestDuration(reply.data());
if (reply->error() == QNetworkReply::NoError) if (reply->error() == QNetworkReply::NoError)
{ {
@@ -219,12 +220,12 @@ namespace BlackCore
this->logReplyErrorMessage(reply.data()); this->logReplyErrorMessage(reply.data());
} }
} }
loop.exit(); if (loop) { loop->exit(); }
} }
}); });
Q_UNUSED(reply) Q_UNUSED(reply)
loop.exec(); if (loop) { loop->exec(); }
return receivedData; return receivedData;
} }
@@ -248,10 +249,10 @@ namespace BlackCore
// posted in QAM thread, reply is nullptr if called from another thread // posted in QAM thread, reply is nullptr if called from another thread
sApp->postToNetwork(request, CApplication::NoLogRequestId, json.toJson(), sApp->postToNetwork(request, CApplication::NoLogRequestId, json.toJson(),
{ {
this, [ & ](QNetworkReply * nwReply) this, [ = ](QNetworkReply * nwReply)
{ {
// called in "this" thread // called in "this" thread
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> reply(nwReply); const QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> reply(nwReply);
if (isShuttingDown()) { return; } if (isShuttingDown()) { return; }
this->logRequestDuration(reply.data()); this->logRequestDuration(reply.data());
if (reply->error() != QNetworkReply::NoError) if (reply->error() != QNetworkReply::NoError)
@@ -276,10 +277,10 @@ namespace BlackCore
// posted in QAM thread // posted in QAM thread
sApp->deleteResourceFromNetwork(request, CApplication::NoLogRequestId, sApp->deleteResourceFromNetwork(request, CApplication::NoLogRequestId,
{ {
this, [ & ](QNetworkReply * nwReply) this, [ = ](QNetworkReply * nwReply)
{ {
// called in "this" thread // called in "this" thread
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> reply(nwReply); const QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> reply(nwReply);
if (isShuttingDown()) { return; } if (isShuttingDown()) { return; }
this->logRequestDuration(reply.data()); this->logRequestDuration(reply.data());
if (reply->error() != QNetworkReply::NoError) if (reply->error() != QNetworkReply::NoError)

View File

@@ -11,10 +11,9 @@
#ifndef BLACKCORE_AFV_CONNECTION_APISERVERCONNECTION_H #ifndef BLACKCORE_AFV_CONNECTION_APISERVERCONNECTION_H
#define BLACKCORE_AFV_CONNECTION_APISERVERCONNECTION_H #define BLACKCORE_AFV_CONNECTION_APISERVERCONNECTION_H
#include "blackmisc/logcategorylist.h"
#include "blackcore/afv/dto.h" #include "blackcore/afv/dto.h"
#include "blackcore/application.h" #include "blackcore/application.h"
#include "blackmisc/logmessage.h" #include "blackmisc/logcategorylist.h"
#include <QString> #include <QString>
#include <QNetworkAccessManager> #include <QNetworkAccessManager>