Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QNetworkReply emits error signal twice when ContentNotFoundError occures when event loop is started in error slot

QNetworkReply emits error signal twice when ContentNotFoundError occures when event loop is started in error slot

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    elonm
    wrote on 4 Oct 2011, 16:04 last edited by
    #1

    Hi all,

    Im using QtSDK 4.7.3

    I am doing this in (void test()):
    @
    mgr = new QNetworkAccessManager();
    reply = mgr->get(QNetworkRequest(QUrl("http://developer.qt.nokia.com/fileNotExisting.txt")));

    connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
    SLOT(onError(QNetworkReply::NetworkError)), Qt::ConnectionType::UniqueConnection);
    @

    And of course the slot onError is called:
    @
    if (networkError == QNetworkReply::NetworkError::ContentNotFoundError)
    {
    // Messagebox starts an event loop which
    // causes this slot to be called again
    QMessageBox m;
    m.exec();
    }
    @

    If i don't have a messagebox/eventloop in the onError slot there is no crash and everything works. But when it is there then the onError slot gets called again when m.exec() is called.
    When both messageboxes are closed and I leave the function onError the application crashes.
    The application tries to delete/free memory when this happens. The error "Access violation reading location" does not help any and the call stack is deep in to Qt dlls.

    What I have checked:
    The signal is not connected twice.
    Tried calling test() before and after the QApplication calls it's exec function. (does not matter)
    Another error like HostNotFound will not call the onError slot twice.
    All my code is executed in the main thread.
    Tried disconnecting the onError slot so it is only called once but it still crashes.
    Tried calling abort on the request in onError().

    Can anyone help me figure out what is happening here?

    Here is the code I use for testing:
    main.cpp
    @#include "contentnotfound.h"
    #include <QtGui/QApplication>
    #include <QTimer>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    ContentNotFound cnf;

    // false: start test after application's event loop have started
    if (true) { cnf.test(); }
    else { QTimer::singleShot(2000, &cnf, SLOT(test())); }

    return a.exec();
    }@

    contentnotfound.h
    @#ifndef CONTENTNOTFOUND_H
    #define CONTENTNOTFOUND_H

    #include <QNetworkAccessManager>
    #include <QNetworkReply>
    #include <QMessageBox>

    class ContentNotFound : public QObject
    {
    Q_OBJECT

    public slots:
    void test()
    {
    mgr = new QNetworkAccessManager();
    reply = mgr->get(QNetworkRequest(QUrl("http://developer.qt.nokia.com/fileNotExisting.txt")));

    connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
    SLOT(onError(QNetworkReply::NetworkError)), Qt::ConnectionType::UniqueConnection);
    }

    private slots:
    void onError(QNetworkReply::NetworkError networkError)
    {
    //reply->disconnect(); // Disconnect all signals

    if (networkError == QNetworkReply::NetworkError::ContentNotFoundError)
    {
    // Messagebox starts an event loop which
    // causes this slot to be called again
    QMessageBox m;
    m.exec();
    }
    }

    private:
    QNetworkAccessManager* mgr;
    QNetworkReply* reply;

    };

    #endif // CONTENTNOTFOUND_H@

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on 4 Oct 2011, 22:27 last edited by
      #2

      I can reproduce the error, but I do not have a solution for it.

      You could test it with the most recent version 4.7.4 of Qt. If the error is still there, I'd suggest you open a bug report in the "public bugtracker":http://bugreports.qt.nokia.com.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • E Offline
        E Offline
        elonm
        wrote on 5 Oct 2011, 14:47 last edited by
        #3

        Thanks for your response. I posted this question on "stackoverflow":http://stackoverflow.com/questions/7650978/qnetworkreply-emits-error-signal-twice-when-contentnotfounderror-occures-when-eve also and got a solution for it.

        To sum it up:

        Replace test() with this:
        @void test()
        {
        qRegisterMetaTypeQNetworkReply::NetworkError("QNetworkReply::NetworkError");
        mgr = new QNetworkAccessManager(this);
        reply = mgr->get(QNetworkRequest(QUrl("http://developer.qt.nokia.com/fileNotExisting.txt")));

        connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
            SLOT(onError(QNetworkReply::NetworkError)), Qt::QueuedConnection);
        

        }@

        There is a bug that will be fixed in the release of Qt version 4.8.0 ("link":https://bugreports.qt.nokia.com/browse/QTBUG-16333?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel)

        1 Reply Last reply
        0

        3/3

        5 Oct 2011, 14:47

        • Login

        • Login or register to search.
        3 out of 3
        • First post
          3/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved