QNetworkAccessManager cause pure virtual method called within QThread

  • Hi, i'm using QNetworkAccessManager within a QThread to download a file but i randomly got a crash. The message is pure virtual method called over the QNetworkAccessManager::get function.
    I know that QNetworkAccessManager is asynchrounous and it should be keept, in the main thread, but the global task i'm doing should be within a thread, and i need to download a file from this thread.

    I found someone having the same problem here: http://stackoverflow.com/questions/10974247/qnetworkaccessmanagerget-cause-pure-virtual-method-called but no solution is provided.

    The following program is reproducing the problem, which happen mainly on Windows 8 and Windows 7. I can't reproduce it over linux and with gdb also. But the stackoverflow's post seems to be able.

    @void WorkerThread::run()
    int iCount = 0;
    while (!m_bStopped) {
    QElapsedTimer timer;

    QDateTime dtNow = QDateTime::currentDateTime();
    qDebug("[Downloading] %s %s", qPrintable(dtNow.toString()), qPrintable(m_szUrl));

    QUrl url = QUrl(m_szUrl);

    QEventLoop loop;
    QByteArray data;

    QNetworkAccessManager* pManager = new QNetworkAccessManager();
    QNetworkRequest request(url);
    request.setRawHeader("User-Agent", "TestProgram");
    QNetworkReply *reply = pManager->get(request); // Here the crash
    connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));


    if (reply->error() != QNetworkReply::NoError) {
    qDebug("[HTTP] Error %d", reply->error());
    if (reply->isRunning())
    int httpCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    if (httpCode != 200){
    qDebug("[HTTP] Http Error %d", httpCode);
    data = reply->readAll();

    delete reply;
    reply = NULL;

    delete pManager;
    pManager = NULL;

    // Avoid memory leak. Deleting the QNetworkAccessManager will finish qt's internal httpThread.
    // This will process the httpThread finished signal, that send deferred delete event on it.
    // So the internal http thread will be deleted when this thread finish.

    qint64 elapsed = timer.elapsed();
    qDebug("[Downloading] Time %ld ms, %ld bytes", (long int)elapsed, (long int)data.size());


    Note: i create each time a new QNetworkAccessManager to reproduce the same conditions.

    I spend many days to try to understand the problem but i don't succeed. I'll hope to find someone with an idea. Thank you very much for you help.

Log in to reply

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