QNetworkAccessManager consistency



  • Two questions about QNetworkAccessManager (Qt 4.8):

    1. In code like the following (apologies for typos, not real code):

    @... {
    QNetworkAccessManager *nam = ...;
    QNetworkReply *reply = nam->get(QNetworkRequest(...));
    connect(reply, SIGNAL(finished(...)), this, SLOT(requestFinished(...)));
    }@

    Is there any chance of a race condition where the request finishes before connect() is called and so "this" never receives the signal?

    1. Will every call to QNetworkAccessManager::get result in a finished() signal, even if there is an error in the request?

    I'm writing a somewhat complicated program to manage a bunch of requests/replies, and it would greatly simplify my code if I know I can connect the reply's finished() signal after calling get() and a) not miss the signal and b) always see a finished() for every request.

    Thanks!


  • Moderators

    [quote author="JCipriani" date="1367019385"]
    @... {
    QNetworkAccessManager *nam = ...;
    QNetworkReply *reply = nam->get(QNetworkRequest(...));
    connect(reply, SIGNAL(finished(...)), this, SLOT(requestFinished(...)));
    }@

    Is there any chance of a race condition where the request finishes before connect() is called and so "this" never receives the signal?[/quote]Nope :) The network request won't actually be sent until the function that calls get() returns. That's because the request gets posted to Qt's event loop, which will wait for your function to return before processing new events. So, as long as you call connect() before the end of your calling function, you're safe.

    [quote]

    1. Will every call to QNetworkAccessManager::get result in a finished() signal, even if there is an error in the request?[/quote]I'm not sure, but you can test it: start a large download using QNetworkAccessManager, and then disconnect your computer from the internet before it finishes. Use qDebug() to tell you if the signals are emitted or not.

  • Moderators

    [quote author="JCipriani" date="1367019385"]

    1. Will every call to QNetworkAccessManager::get result in a finished() signal, even if there is an error in the request?[/quote]
      yep it will.


  • Great, thanks guys.

    I'm observing memory leaks every time I make an HTTP get request that ultimately leads to the program crashing. I'm pretty sure I'm doing everything "right", but it could be an issue with my code. I'm currently investigating and working on a simpler test case. In the mean time, are there any known issues with QNetworkAccessManager memory leaks in Qt 4.8 (Windows 7)? I see a couple of Google results, but nothing specific, and not a lot of results. I found https://bugreports.qt-project.org/browse/QTBUG-30817 but it's fairly generic. I will post here or start a new thread if I can narrow it down.

    Thanks!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.