Results 1 to 3 of 3

Thread: How to properly stop QNetworkRequest ?

  1. #1
    Join Date
    Aug 2015
    Location
    Poland/UK
    Posts
    30
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to properly stop QNetworkRequest ?

    Hi, I've got a problem with my HttpClient, when I'm trying to stop/cancel request my application crashes... How can I cancel it properly ?

    After when I'm trying to stop by this function:
    Qt Code:
    1. void HttpClient::stop(){
    2. if(m_reply) {
    3. m_reply->abort();
    4. }
    5. }
    To copy to clipboard, switch view to plain text mode 

    it crashes on: loop.exec();

    in function 'wait for finish'...

    Qt Code:
    1. void HttpClient::waitForFinish(QNetworkReply *reply)
    2. {
    3. QEventLoop loop;
    4. connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
    5. loop.exec();
    6. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. QString HttpClient::get(const QString &url)
    2. {
    3. if(url.isEmpty()){
    4. Logger::getInstance()->Error(trUtf8("HttpClient: GET Response <Url is empty!>"));
    5. }
    6. m_reply = m_manager->get(*this->generateRequest(url, false));
    7. waitForFinish(m_reply);
    8. if(!m_reply){
    9. return "";
    10. }
    11.  
    12. ... code
    To copy to clipboard, switch view to plain text mode 

    This problem occurs sometimes, not always. I think that if, in another thread still performs and here is aborted it causes this crash...
    Any idea ?

    Thanks for any help

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to properly stop QNetworkRequest ?

    Since you are using a nested event loop, have you checked if you enter the get() method maybe more than once?

    Also the * at this->generateRequest looks peculiar, almost like if the return value of generateRequest is QUrl* and not QUrl.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to properly stop QNetworkRequest ?

    You mentioned threads. Are HttpClient::stop() and HttpClient::get() executed by distinct threads? That would be a problem.

Similar Threads

  1. Console Application cannot stop properly a thread
    By erakis in forum Qt Programming
    Replies: 28
    Last Post: 3rd January 2015, 00:33
  2. Replies: 4
    Last Post: 1st October 2013, 09:33
  3. QNetworkAccessManager.get(QNetworkRequest) not working.. :(
    By matthieunc in forum Qt Programming
    Replies: 4
    Last Post: 23rd July 2011, 14:18
  4. QNetworkRequest Proxy
    By Ricardo_arg in forum Qt Programming
    Replies: 2
    Last Post: 12th April 2011, 19:50
  5. QNetworkRequest atribute
    By mero in forum Qt Programming
    Replies: 23
    Last Post: 13th March 2011, 00:02

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.