Don't crash when the receiver of a network get is destroyed before the request finishes
This commit is contained in:
parent
547a009d73
commit
d09bae16ee
|
@ -22,6 +22,7 @@ NetworkAccessManager::NetworkAccessManager(QObject* parent,
|
|||
|
||||
void NetworkAccessManager::Get(const QUrl &url, QObject *receiver,
|
||||
const char *slot, quint64 id, bool force_cache) {
|
||||
connect(receiver, SIGNAL(destroyed(QObject*)), SLOT(ReceiverDestroyed(QObject*)));
|
||||
QMetaObject::invokeMethod(
|
||||
this, "RunGet", Qt::QueuedConnection,
|
||||
Q_ARG(QUrl, url), Q_ARG(QObject*, receiver),
|
||||
|
@ -43,6 +44,13 @@ void NetworkAccessManager::RunGet(const QUrl &url, QObject *receiver,
|
|||
pending_replies_.insert(reply, r);
|
||||
}
|
||||
|
||||
void NetworkAccessManager::ReceiverDestroyed(QObject* receiver) {
|
||||
foreach (QNetworkReply* key, pending_replies_.keys()) {
|
||||
if (pending_replies_[key].receiver == receiver)
|
||||
pending_replies_[key].receiver = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
QNetworkRequest NetworkAccessManager::CreateRequest(const QUrl& url, bool force_cache) {
|
||||
QNetworkRequest req(url);
|
||||
req.setRawHeader("User-Agent", QString("%1 %2").arg(
|
||||
|
@ -60,6 +68,9 @@ void NetworkAccessManager::RequestFinished() {
|
|||
QNetworkReply* reply = static_cast<QNetworkReply*>(sender());
|
||||
Receiver r = pending_replies_.take(reply);
|
||||
|
||||
if (!r.receiver)
|
||||
return;
|
||||
|
||||
QMetaObject::invokeMethod(r.receiver, r.slot,
|
||||
Q_ARG(quint64, r.id),
|
||||
Q_ARG(QNetworkReply*, reply));
|
||||
|
|
|
@ -31,6 +31,7 @@ class NetworkAccessManager : public QObject {
|
|||
quint64 id, bool force_cache);
|
||||
void RunGetBlocking(const QUrl& url, bool force_cache, QNetworkReply** reply);
|
||||
void RequestFinished();
|
||||
void ReceiverDestroyed(QObject* receiver);
|
||||
|
||||
private:
|
||||
QNetworkRequest CreateRequest(const QUrl& url, bool force_cache);
|
||||
|
|
Loading…
Reference in New Issue