QByteArray being corrupted in a custom class
-
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 initializem_databut rather add data to it withappend(), 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!
-
@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?
@TheVancedGamer No, that's a regular copy constructor - which only increments a reference count as
QByteArrayis implicitly shared.The symptom you describe makes me think that the original
QByteArrayyou created and eventually passed to theMessageconstructor doesn't own its' buffer - that is, was created with something likeQByteArray::fromRawDataor the like. -
Did you init it with another non-const
QByteArrayvariable? 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.@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?
-
@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?
@TheVancedGamer No, that's a regular copy constructor - which only increments a reference count as
QByteArrayis implicitly shared.The symptom you describe makes me think that the original
QByteArrayyou created and eventually passed to theMessageconstructor doesn't own its' buffer - that is, was created with something likeQByteArray::fromRawDataor the like. -
@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?
-
@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?
@TheVancedGamer
QByteArrayuses 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. -
@TheVancedGamer No, that's a regular copy constructor - which only increments a reference count as
QByteArrayis implicitly shared.The symptom you describe makes me think that the original
QByteArrayyou created and eventually passed to theMessageconstructor doesn't own its' buffer - that is, was created with something likeQByteArray::fromRawDataor the like.@IgKh That was it! I forgot I was hacking around and that did it! Thank you!
-
T TheVancedGamer has marked this topic as solved on