Results 1 to 6 of 6

Thread: QNetworkReply app crash in QThread

  1. #1
    Join Date
    May 2011
    Posts
    132
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QNetworkReply app crash in QThread

    I create an QThread using moveToThread(), where I use a QTimer to pull stuff from network instantly.

    In construct I call
    Qt Code:
    1. _http = new HttpAdapter(this);
    To copy to clipboard, switch view to plain text mode 

    and in periodically function run
    Qt Code:
    1. _http->Download();
    To copy to clipboard, switch view to plain text mode 

    In httpAdapter header
    Qt Code:
    1. QNetworkReply* _reply;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void HttpAdapter::Download() {
    2.  
    3. QString url;
    4. url += Server::get()->GetBaseUrl();
    5. url += "get.xml";
    6.  
    7. QNetworkAccessManager * http = new QNetworkAccessManager;
    8.  
    9. QNetworkRequest request;
    10. request.setHeader(QNetworkRequest::ContentLengthHeader, "0");
    11. request.setUrl(QUrl::fromUserInput(url));
    12.  
    13. _reply = http->get(request);
    14.  
    15. connect(_reply,SIGNAL(finished()),this, SLOT(Finished()));
    16. connect(_reply,SIGNAL(readyRead()),this, SLOT(ReadyData()));
    17. connect(_reply,SIGNAL(error(QNetworkReply::NetworkError)),this, SLOT(foldersError(QNetworkReply::NetworkError)));
    18. connect(_reply,SIGNAL(sslErrors ( const QList<QSslError> & )),this, SLOT(SslErrors( const QList<QSslError> &)));
    19. }
    To copy to clipboard, switch view to plain text mode 

    The on finish...

    Qt Code:
    1. void HttpAdapter::Finished() {
    2. DEBUGME_MSG("finished");
    3.  
    4. int httpCode = _reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ).toInt();
    5. if (httpCode != 200) {
    6. INFO_DEBUGME("HTTP CODE " + QString::number(httpCode));
    7. return;
    8. }
    9.  
    10. if (!_reply->isFinished()) {
    11. return;
    12. }
    13.  
    14. if (!_reply->isReadable()) {
    15. return;
    16. }
    17.  
    18. _data = _reply->readAll();
    19. _reply->deleteLater();
    20.  
    21. if (_data.isEmpty()) {
    22. INFO_DEBUGME("DATA EMPTY");
    23. return;
    24. }
    25.  
    26. //success
    27. }
    To copy to clipboard, switch view to plain text mode 

    And THE CRASH IS ON
    Qt Code:
    1. _data = _reply->readAll();
    To copy to clipboard, switch view to plain text mode 

    The crash happen randomly, I would say in 5% of the calls, Any idea why ??

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkReply app crash in QThread

    Get rid of the memory leak related to creating the access manager object at every iteration and then see if the problem persists.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2011
    Posts
    132
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QNetworkReply app crash in QThread

    How do I do it ?

    QNetworkAccessManager * http = new QNetworkAccessManager;

    on finished

    http->deleteLater() ?

    I have tried

    Qt Code:
    1. delete _reply->manager();
    To copy to clipboard, switch view to plain text mode 
    always crash
    Last edited by migel; 29th August 2011 at 17:53.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkReply app crash in QThread

    Quote Originally Posted by migel View Post
    How do I do it ?
    You rewrite the code so it doesn't leak memory. For example by reusing the same object.

    always crash
    Well, if "reply" is invalid then "reply->manager()" will surely be invalid as well.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    May 2011
    Posts
    132
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QNetworkReply app crash in QThread

    I have put it that to the construct
    Qt Code:
    1. _http = new QNetworkAccessManager;
    To copy to clipboard, switch view to plain text mode 

    So I am using same object every call, app crashed on readAll() again after few minutes.

    Any clues ?
    Last edited by migel; 29th August 2011 at 22:48.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkReply app crash in QThread

    You need to provide us with more complete code. I'm especially interested in when and how you call this Download() method. My guess is that when finished() is emitted, your reply is already invalid or overwritten by something else.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 2
    Last Post: 1st February 2011, 22:52
  2. Subclassing QNetworkReply
    By piotr.dobrogost in forum Qt Programming
    Replies: 6
    Last Post: 19th December 2010, 09:42
  3. QThread::wait() causing crash
    By Olliebrown in forum Qt Programming
    Replies: 3
    Last Post: 24th September 2010, 16:24
  4. QNetworkReply::abort crash
    By danc81 in forum Qt Programming
    Replies: 0
    Last Post: 4th November 2009, 12:30
  5. QThread issues. Crash after 2058 runs.
    By zverj in forum Qt Programming
    Replies: 4
    Last Post: 15th October 2009, 11:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.