Since you create new QNetworkAccessManager
instance each time MyApp::getData()
called, you need to manage to delete the right one in MyApp::onRequestCompleted()
function. The problem in your code is that you always delete the last created QNetworkAccessManager
and not the one that caused the slot call. My suggestion is following:
void MyApp::getData() {
[..]
// Not class member. Will be deleted in the slot.
QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
connect(m_networkManager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(onRequestCompleted(QNetworkReply *)));
[..]
}
void MyApp::onRequestCompleted(QNetworkReply *reply) {
QByteArray data = reply->readAll();
reply->deleteLater();
// Delete object whose signal triggered this slot.
QObject *networkManager = sender();
networkManager->deleteLater();
}