Don't crash when the receiver of a network get is destroyed before the request finishes

This commit is contained in:
David Sansome 2010-10-09 14:15:05 +00:00
parent 547a009d73
commit d09bae16ee
2 changed files with 12 additions and 0 deletions

View File

@ -22,6 +22,7 @@ NetworkAccessManager::NetworkAccessManager(QObject* parent,
void NetworkAccessManager::Get(const QUrl &url, QObject *receiver, void NetworkAccessManager::Get(const QUrl &url, QObject *receiver,
const char *slot, quint64 id, bool force_cache) { const char *slot, quint64 id, bool force_cache) {
connect(receiver, SIGNAL(destroyed(QObject*)), SLOT(ReceiverDestroyed(QObject*)));
QMetaObject::invokeMethod( QMetaObject::invokeMethod(
this, "RunGet", Qt::QueuedConnection, this, "RunGet", Qt::QueuedConnection,
Q_ARG(QUrl, url), Q_ARG(QObject*, receiver), 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); 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 NetworkAccessManager::CreateRequest(const QUrl& url, bool force_cache) {
QNetworkRequest req(url); QNetworkRequest req(url);
req.setRawHeader("User-Agent", QString("%1 %2").arg( req.setRawHeader("User-Agent", QString("%1 %2").arg(
@ -60,6 +68,9 @@ void NetworkAccessManager::RequestFinished() {
QNetworkReply* reply = static_cast<QNetworkReply*>(sender()); QNetworkReply* reply = static_cast<QNetworkReply*>(sender());
Receiver r = pending_replies_.take(reply); Receiver r = pending_replies_.take(reply);
if (!r.receiver)
return;
QMetaObject::invokeMethod(r.receiver, r.slot, QMetaObject::invokeMethod(r.receiver, r.slot,
Q_ARG(quint64, r.id), Q_ARG(quint64, r.id),
Q_ARG(QNetworkReply*, reply)); Q_ARG(QNetworkReply*, reply));

View File

@ -31,6 +31,7 @@ class NetworkAccessManager : public QObject {
quint64 id, bool force_cache); quint64 id, bool force_cache);
void RunGetBlocking(const QUrl& url, bool force_cache, QNetworkReply** reply); void RunGetBlocking(const QUrl& url, bool force_cache, QNetworkReply** reply);
void RequestFinished(); void RequestFinished();
void ReceiverDestroyed(QObject* receiver);
private: private:
QNetworkRequest CreateRequest(const QUrl& url, bool force_cache); QNetworkRequest CreateRequest(const QUrl& url, bool force_cache);