Results 1 to 5 of 5

Thread: Application crash after 2 entries to requestFinished function

  1. #1
    Join Date
    Mar 2008
    Posts
    55
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Application crash after 2 entries to requestFinished function

    Hello everyone, I've developped an application whitch send a Web service to the server.
    For the response, I've user the requestFinished slot. But I've seen that my application enter 2 to this function.
    This causes the crash of my application and I've the message shown in the PJ
    Can anyone tell me what's wrong ?
    Many thanks in advance.
    Best regards.
    Attached Images Attached Images

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Application crash after 2 entries to requestFinished function

    Most likely you are directly or indirectly using a null or invalid pointer. No code, no further help.

    You should be using QNetworkAccessManager instead of QHttp which, as the fine manual says, is:
    This class is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.

  3. #3
    Join Date
    Mar 2008
    Posts
    55
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Application crash after 2 entries to requestFinished function

    Many Thanks for replying. It's right I've not helped you with more details.
    In fact, my application which should connect to the server is
    Qt Code:
    1. void Connexion::connectToWebService(QString host, quint16 port, QString path, vector <QStringList> paramsList)
    2. {
    3. // formater la chaine à envoyer
    4. QString params;
    5. int i = 0;
    6. int listParamSize = paramsList.size()-1;
    7. while(i <= listParamSize){
    8. if(0 < i){
    9. params += "&";
    10. }
    11. params += QString(paramsList[i].at(0).toLocal8Bit().constData())+"="+QUrl::toPercentEncoding(QString(paramsList[i].at(1).toLocal8Bit().constData()));
    12. ++i;
    13. }
    14.  
    15. //invoquer le service web
    16. this->http.clearPendingRequests();
    17.  
    18. QHttpRequestHeader header("POST", path);
    19. header.setValue("HOST", host);
    20. header.setValue("Cache-Control", "no-cache");
    21. header.setContentType("application/x-www-form-urlencoded");
    22. header.setContentLength(params.length());
    23.  
    24. this->http.setHost(host);
    25.  
    26. this->idVerify = this->http.request(header, params.toUtf8());
    27.  
    28. this->http.closeConnection();
    To copy to clipboard, switch view to plain text mode 
    The http is not declared as QHttp http; in .h file (not as pointer) and I've connected it like
    Qt Code:
    1. connect(&http, SIGNAL(requestFinished(int, bool)), this, SLOT(receive(int, bool)));
    To copy to clipboard, switch view to plain text mode 
    After that, I've implemented the functionc receive witch connected to the requestFinished slot like
    Qt Code:
    1. void Connexion::receive(int id, bool error)
    2. {
    3. if(error)
    4. {
    5. qDebug() << http.errorString();
    6. this->result = http.errorString();
    7. }
    8. else
    9. {
    10. if(id == this->idVerify )
    11. {
    12. QTextCodec *textCodec = QTextCodec::codecForName("utf-8");
    13. QString res = textCodec->toUnicode(this->http.readAll());
    14.  
    15. doc.setContent(res);
    16.  
    17. QDomElement docElem = doc.documentElement();
    18. result = docElem.text();
    19.  
    20. // fonction à appeler après reception données
    21. startApplication();
    22. }
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 
    Note that in the receive function, I call a function called startApplication and in last one I show a messageBox if the WebService returned an error.
    But after displying the messageBox, I wait some time and the my application access to the receive function another time and a second messageBox is shown. If I click Ok on the 2 messages the application crashes.
    I've tried some solutions but everytime it does not work :
    1. Use http like a pointer and delete it if a have an error but it does'nt work.
    2. Using a bool variable to deny the second access to receive function
    I wish it is more clair now and anyone can help me.
    Best regards.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Application crash after 2 entries to requestFinished function

    Look at the backtrace in your debugger. Which line is actually triggering the seg fault?

    You issue the request and immediately command the connection to close (using a obsolete method in the obsolete class!). When do you receive() the requestFinished() signal for the close, and does that happen before you receive the reply to your main request?

    The length of the params QString (your Content-length) and the length of that string encoded as UTF8 (i.e. your data payload) are not necessarily the same. For example;
    Qt Code:
    1. QString a = QString::fromUtf8("Je parle très peu français.");
    2. qDebug() << a << a.length();
    3. QByteArray utf8 = a.toUtf8();
    4. qDebug() << utf8.length() << utf8.toHex();
    To copy to clipboard, switch view to plain text mode 
    27 characters encoded in 29 bytes.
    Qt Code:
    1. 4a 65 20 70 61 72 6c 65 20 74 72 c3 a8 73 20 70 65 75 20 66 72 61 6e c3 a7 61 69 73 2e
    2. J e sp p a r l e sp t r è s sp p e u sp f r a n ç a i s .
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to ChrisW67 for this useful post:

    mourad (21st October 2011)

  6. #5
    Join Date
    Mar 2008
    Posts
    55
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Application crash after 2 entries to requestFinished function

    Well, I've modified my code and it works now.
    1. I've not closed the http connexion (I've seen that it is automatiquely done).
    2. I've gotten the request response in the requestFinished slot when the id is equal to the sended resquest.
    3. I've used the done slot to call my traitement if there is no error

    For more information, you can seen the stateChanged slot.

    Regards.

Similar Threads

  1. Replies: 3
    Last Post: 25th April 2011, 03:40
  2. Replies: 10
    Last Post: 9th July 2009, 12:05
  3. Replies: 1
    Last Post: 9th July 2009, 08:59
  4. Problem with QHttp requestFinished
    By jdd81 in forum Qt Programming
    Replies: 0
    Last Post: 6th November 2008, 23:31
  5. Weird application crash
    By MarkoSan in forum Qt Programming
    Replies: 10
    Last Post: 20th May 2008, 14: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
  •  
Qt is a trademark of The Qt Company.