Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QByteArray strange behavior



  • Hello Everyone,

    I'm new to qt and I'm facing some issue with QByteArray. All of a sudden its acting strangely.

    Ex:

    void  ClassName::OnResponseRecieved(QNetwrokReply*  reply)
    {
        qDebug() <<  reply->readAll();  //Prints the response on console "Response"
        thread.ParseResponse( reply->readAll(););
    }
    

    The above method is a slot which handles all the server response of my application. Here when trying to display the response I got, its displaying correctly on the console. I'm passing this response to a thread to process it.

    void ThreadClass::ParseResponse(const QByteArray& responseData)
    {
       qDebug() << responseData;
      // Other stuff
    }
    

    Here when trying to print the responseData its showing empty.

    FINAL OUTPUT

    "Server Response"  <- Debug Statement 1
    ""                <- Debug Statement 2
    

    It used to work properly but for some reason it started acting strangely. All the necessary header files are include. Had cleaned and rebuild the code multiple times but the issue is still there.


  • Lifetime Qt Champion

    @Abhi_Varma said in QByteArray strange behavior:

    void ClassName::OnResponseRecieved(QNetwrokReply* reply)
    {
    qDebug() << reply->readAll(); //Prints the response on console "Response"
    thread.ParseResponse( reply->readAll(););
    }

    This is completely wrong!
    The first readAll() call for debug will read what is currently available for reading.
    The second call will read what is remaining for reading.
    No wonder it is not working.
    It should be

    void  ClassName::OnResponseRecieved(QNetwrokReply*  reply)
    {
        QByteArray temp = reply->readAll();
        qDebug() <<  temp;  //Prints the response on console "Response"
        thread.ParseResponse(temp);
    }
    

  • Lifetime Qt Champion

    @Abhi_Varma Don't pass the array by reference to a thread! responseData is a local variable! Pass it by value. Also, I don't know how ParseResponse works, but you are calling it in the same thread!



  • Updated code slightly.

    @jsulm ParseMessage is used to push data into a queue that's it. As server continuously send data first it's pushed into queue, later in run its processed.

    Passing it as reference is working most of the time and same goes with pass by value works sometimes.

    OnResponseRecieved is invoked multiple times as there are multiple things asking for server for data but like i said it doesnt happen everytime and not for all server responses.



  • People, need help on this issue.

    My Settings:
    OS: Windows 10 64bit
    Qt Version: 5.15.2
    Compiler: MSVC 2019

    This issue is bothering me so much. No matter how many I rebuild its not working. Yesterday it worked for sometime and again stopped working


  • Lifetime Qt Champion

    @Abhi_Varma said in QByteArray strange behavior:

    void ClassName::OnResponseRecieved(QNetwrokReply* reply)
    {
    qDebug() << reply->readAll(); //Prints the response on console "Response"
    thread.ParseResponse( reply->readAll(););
    }

    This is completely wrong!
    The first readAll() call for debug will read what is currently available for reading.
    The second call will read what is remaining for reading.
    No wonder it is not working.
    It should be

    void  ClassName::OnResponseRecieved(QNetwrokReply*  reply)
    {
        QByteArray temp = reply->readAll();
        qDebug() <<  temp;  //Prints the response on console "Response"
        thread.ParseResponse(temp);
    }
    

  • Moderators

    @Abhi_Varma

    are you sure, you're not simply running out of memory? I don't see you deleting the QNetworkReply instances anywhere in your code



  • @jsulm I just now noticed this when I was debugging the code and was about to update here but I see u have already answered it. Thanks


Log in to reply