Fix: now the old_payload_data is a buffer to the original Payload data instead of...
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Fri, 6 Jan 2012 10:09:09 +0000 (08:09 -0200)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Fri, 6 Jan 2012 10:09:09 +0000 (08:09 -0200)
- This is required because the Payload.reset() deletes the data pointed by the old_payload_data,
so it became invalid and was breaking the append() method.

src/host/messagepayload.cpp

index cb00b1e..8a46bae 100644 (file)
@@ -160,15 +160,24 @@ void MessagePayload::append(
     {
         BOOST_ASSERT( 0 < extra_payload_size_in_bytes );
 
+        // as Payload.reset() deletes the array previously held by Payload, we
+        // can't reference that array by old_payload_data pointer, thus it is
+        // required to copy the old array to another memory location in order to
+        // keep the original data intact
         size_t old_payload_size_in_bytes = PayloadSizeInBytes;
-        uint8_t *old_payload_data = Payload.get();
+        uint8_t *old_payload_data = new uint8_t[ old_payload_size_in_bytes ];
+        copy( Payload.get(), Payload.get() + PayloadSizeInBytes, old_payload_data );
+        scoped_array<uint8_t> old_payload( old_payload_data );
 
         size_t new_payload_size_in_bytes = old_payload_size_in_bytes + extra_payload_size_in_bytes;
         uint8_t *new_payload_data = new uint8_t[ new_payload_size_in_bytes ];
 
+        // copy both data, the original and the one to be appended, to the new
+        // buffer, in contiguous memory.
         copy( &old_payload_data[0], &old_payload_data[old_payload_size_in_bytes], &new_payload_data[0] );
         copy( &extra_payload_data[0], &extra_payload_data[extra_payload_size_in_bytes], &new_payload_data[old_payload_size_in_bytes] );
 
+        // the reset() deletes the old array held by Payload
         Payload.reset( new_payload_data );
         PayloadSizeInBytes = new_payload_size_in_bytes;