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 being corrupted in a custom class
Forum Updated to NodeBB v4.3 + New Features

QByteArray being corrupted in a custom class

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 739 Views 1 Watching
  • 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.
  • T Offline
    T Offline
    TheVancedGamer
    wrote on last edited by
    #1

    Hi all. I am writing a Qt app and I have made a custom class for serializing/deserializing a message to be sent to the device.

    class Message {
    public:
        enum MessageType {
            ACK       = 0x01,
            COMMAND_1 = 0x0c,
            COMMAND_2 = 0x0e
        };
    
        MessageType type() const;
        int sequence() const;
        QByteArray data() const;
    
        QByteArray encode();
        QByteArray encode(int sequenceNumber);
    
        static quint32 payloadLength(const QByteArray &data);
    
    private:
        MessageType m_type;
        int m_sequence;
    
        QByteArray m_data;
    };
    

    Now I have to encode the message when sending it, so I store these Message's in a QQueue. But when I dequeue the message to encode it, QByteArray is magically corrupted! It loses all of the data but all of the other member variables are fine. And interestingly, if I don't initialize m_data but rather add data to it with append(), the corruption goes away!

    I have been pulling my hair out for the past week and I have no idea why this happens, any ideas would be greatly appreciated!

    1 Reply Last reply
    0
    • T TheVancedGamer

      @Bonnie I mean, I just initialized it with the variable as in the constructor like this:

      Message::Message(MessageType type, int sequenceNumber, QByteArray payload):
        m_type(type),
        m_sequence(sequenceNumber),
        m_data(payload)
      {
          //m_data.append(payload);
      }
      

      This probably did the move-constructor?

      I Offline
      I Offline
      IgKh
      wrote on last edited by
      #4

      @TheVancedGamer No, that's a regular copy constructor - which only increments a reference count as QByteArray is implicitly shared.

      The symptom you describe makes me think that the original QByteArray you created and eventually passed to the Message constructor doesn't own its' buffer - that is, was created with something like QByteArray::fromRawData or the like.

      T 1 Reply Last reply
      4
      • B Offline
        B Offline
        Bonnie
        wrote on last edited by
        #2

        Did you init it with another non-const QByteArray variable? According to the doc:

        QByteArray::QByteArray(QByteArray &&other)
        Move-constructs a QByteArray instance, making it point at the same object that other was pointing to.

        T 1 Reply Last reply
        0
        • B Bonnie

          Did you init it with another non-const QByteArray variable? According to the doc:

          QByteArray::QByteArray(QByteArray &&other)
          Move-constructs a QByteArray instance, making it point at the same object that other was pointing to.

          T Offline
          T Offline
          TheVancedGamer
          wrote on last edited by
          #3

          @Bonnie I mean, I just initialized it with the variable as in the constructor like this:

          Message::Message(MessageType type, int sequenceNumber, QByteArray payload):
            m_type(type),
            m_sequence(sequenceNumber),
            m_data(payload)
          {
              //m_data.append(payload);
          }
          

          This probably did the move-constructor?

          I B Axel SpoerlA 3 Replies Last reply
          0
          • T TheVancedGamer

            @Bonnie I mean, I just initialized it with the variable as in the constructor like this:

            Message::Message(MessageType type, int sequenceNumber, QByteArray payload):
              m_type(type),
              m_sequence(sequenceNumber),
              m_data(payload)
            {
                //m_data.append(payload);
            }
            

            This probably did the move-constructor?

            I Offline
            I Offline
            IgKh
            wrote on last edited by
            #4

            @TheVancedGamer No, that's a regular copy constructor - which only increments a reference count as QByteArray is implicitly shared.

            The symptom you describe makes me think that the original QByteArray you created and eventually passed to the Message constructor doesn't own its' buffer - that is, was created with something like QByteArray::fromRawData or the like.

            T 1 Reply Last reply
            4
            • T TheVancedGamer

              @Bonnie I mean, I just initialized it with the variable as in the constructor like this:

              Message::Message(MessageType type, int sequenceNumber, QByteArray payload):
                m_type(type),
                m_sequence(sequenceNumber),
                m_data(payload)
              {
                  //m_data.append(payload);
              }
              

              This probably did the move-constructor?

              B Offline
              B Offline
              Bonnie
              wrote on last edited by Bonnie
              #5

              @TheVancedGamer I think I was wrong. You probably need to init it with a reference variable to call the move-constructor.
              @IgKh Right, this makes sense. In that case, deep copy by append() is the correct way.

              1 Reply Last reply
              0
              • T TheVancedGamer

                @Bonnie I mean, I just initialized it with the variable as in the constructor like this:

                Message::Message(MessageType type, int sequenceNumber, QByteArray payload):
                  m_type(type),
                  m_sequence(sequenceNumber),
                  m_data(payload)
                {
                    //m_data.append(payload);
                }
                

                This probably did the move-constructor?

                Axel SpoerlA Offline
                Axel SpoerlA Offline
                Axel Spoerl
                Moderators
                wrote on last edited by
                #6

                @TheVancedGamer
                QByteArray uses implicit sharing, with copy-on-write. Your append-trick is one option to force a copy. It's better, however, to make the c'tor argument const-ref, i.e. const QByteArray &payload.

                Software Engineer
                The Qt Company, Oslo

                1 Reply Last reply
                1
                • I IgKh

                  @TheVancedGamer No, that's a regular copy constructor - which only increments a reference count as QByteArray is implicitly shared.

                  The symptom you describe makes me think that the original QByteArray you created and eventually passed to the Message constructor doesn't own its' buffer - that is, was created with something like QByteArray::fromRawData or the like.

                  T Offline
                  T Offline
                  TheVancedGamer
                  wrote on last edited by
                  #7

                  @IgKh That was it! I forgot I was hacking around and that did it! Thank you!

                  1 Reply Last reply
                  0
                  • T TheVancedGamer has marked this topic as solved on

                  • Login

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