Skip to content

Commit

Permalink
Refs #21121: Fix Loan ser_payload destruction
Browse files Browse the repository at this point in the history
Signed-off-by: cferreiragonz <carlosferreira@eprosima.com>
  • Loading branch information
cferreiragonz committed Jun 7, 2024
1 parent 0ab0a38 commit 0928c99
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
40 changes: 40 additions & 0 deletions include/fastdds/rtps/common/SerializedPayload.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -115,6 +122,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
Expand Down
7 changes: 7 additions & 0 deletions src/cpp/fastdds/publisher/DataWriterImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 0928c99

Please sign in to comment.