diff --git a/include/fastdds/rtps/common/SerializedPayload.h b/include/fastdds/rtps/common/SerializedPayload.h index 373924c36ec..ca535e34aac 100644 --- a/include/fastdds/rtps/common/SerializedPayload.h +++ b/include/fastdds/rtps/common/SerializedPayload.h @@ -83,6 +83,13 @@ struct FASTDDS_EXPORTED_API SerializedPayload_t { } + //!Copy constructor + SerializedPayload_t( + const SerializedPayload_t& other) = default; + //!Copy operator + SerializedPayload_t& operator = ( + const SerializedPayload_t& other) = default; + /** * @param len Maximum size of the payload */ @@ -114,6 +121,39 @@ struct FASTDDS_EXPORTED_API SerializedPayload_t (0 == memcmp(data, other.data, length))); } + //!Move operator + SerializedPayload_t& operator = ( + SerializedPayload_t&& other) noexcept + { + if (this == &other) + { + return *this; + } + + encapsulation = other.encapsulation; + length = other.length; + data = other.data; + max_size = other.max_size; + pos = other.pos; + payload_owner = other.payload_owner; + + other.encapsulation = CDR_BE; + other.length = 0; + other.data = nullptr; + other.max_size = 0; + other.pos = 0; + other.payload_owner = nullptr; + + return *this; + } + + //!Move constructor + SerializedPayload_t( + SerializedPayload_t&& other) noexcept + { + *this = std::move(other); + } + /*! * Copy another structure (including allocating new space for the data). * @param[in] serData Pointer to the structure to copy diff --git a/src/cpp/fastdds/publisher/DataWriterImpl.cpp b/src/cpp/fastdds/publisher/DataWriterImpl.cpp index d886ecee710..3eba13c88fb 100644 --- a/src/cpp/fastdds/publisher/DataWriterImpl.cpp +++ b/src/cpp/fastdds/publisher/DataWriterImpl.cpp @@ -112,6 +112,9 @@ class DataWriterImpl::LoanCollection if (it->data == payload_data) { payload = *it; + // Avoid releasing the payload in destructor + it->payload_owner = nullptr; + it->data = nullptr; loans_.erase(it); return true; } @@ -564,6 +567,10 @@ ReturnCode_t DataWriterImpl::loan_sample( break; } + // Avoid releasing the payload in destructor + payload.payload_owner = nullptr; + payload.data = nullptr; + return RETCODE_OK; }