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. QByteArray strange behavior
QtWS25 Last Chance

QByteArray strange behavior

Scheduled Pinned Locked Moved Solved General and Desktop
qbytearrayqthread
7 Posts 3 Posters 659 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.
  • A Offline
    A Offline
    Abhi_Varma
    wrote on 31 May 2021, 07:14 last edited by Abhi_Varma 6 Jan 2021, 03:48
    #1

    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.

    J 2 Replies Last reply 31 May 2021, 07:17
    0
    • A Abhi_Varma
      31 May 2021, 07:14

      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.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 1 Jun 2021, 06:10 last edited by
      #5

      @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);
      }
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply 1 Jun 2021, 06:47
      2
      • A Abhi_Varma
        31 May 2021, 07:14

        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.

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 31 May 2021, 07:17 last edited by
        #2

        @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!

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        A 1 Reply Last reply 1 Jun 2021, 03:38
        1
        • J jsulm
          31 May 2021, 07:17

          @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!

          A Offline
          A Offline
          Abhi_Varma
          wrote on 1 Jun 2021, 03:38 last edited by Abhi_Varma 6 Jan 2021, 05:40
          #3

          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.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Abhi_Varma
            wrote on 1 Jun 2021, 06:02 last edited by
            #4

            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

            1 Reply Last reply
            0
            • A Abhi_Varma
              31 May 2021, 07:14

              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.

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 1 Jun 2021, 06:10 last edited by
              #5

              @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);
              }
              

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              A 1 Reply Last reply 1 Jun 2021, 06:47
              2
              • J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 1 Jun 2021, 06:12 last edited by
                #6

                @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


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                0
                • J jsulm
                  1 Jun 2021, 06:10

                  @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);
                  }
                  
                  A Offline
                  A Offline
                  Abhi_Varma
                  wrote on 1 Jun 2021, 06:47 last edited by
                  #7

                  @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

                  1 Reply Last reply
                  0

                  7/7

                  1 Jun 2021, 06:47

                  • Login

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