After I serialized the ros2 message and extracted the buffer data, When I deserialize as follows,It has the following problem:terminate called after throwing an instance of 'rcpputils::IllegalStateException' what(): Wrongly initialized. Serialized message has a size of zero. [ros2run]: Aborted
void callbackCloudResponseMessage(const planning_interface::msg::Cloud & msg) {
/* serialize */
rclcpp::Serialization<planning_interface::msg::Cloud> cloudSerializer;
rclcpp::SerializedMessage serialized_msg;
cloudSerializer.serialize_message(&msg, &serialized_msg);
uint8_t* serialized_data = serialized_msg.get_rcl_serialized_message().buffer;
size_t serialized_size = serialized_msg.size();
static uint8_t buf_test[FRAME_LENGTH_MAX+1];
uint8_t *pmsg_test = buf_test + DATA_PAYLOAD;
memcpy(pmsg_test, serialized_data, serialized_size);
/* deserialize */
planning_interface::msg::Cloud test_msg;
rclcpp::SerializedMessage serialize_test_msg(serialized_size);
memcpy(new_msg.get_rcl_serialized_message().buffer, pmsg_test, serialized_size);
cloudSerializer.deserialize_message(&new_msg, &test_msg);
}
I tried the above method and expected to be able to deserialize
(I assume that
serialize_test_msg
is just an unfinished rename ofnew_msg
or vice versa which you did after run, otherwise your example wouldn't compile in the first place)The problem is that
new_msg
is zero size when you calldeserialize_message
becauserclcpp::SerializedMessage
constructor you use takessize_t
as capacity, not size, of the created buffer. It allocates memory (to which you then successfullymemcpy
) and setsbuffer
,buffer_capacity
andallocator
fields of the underlying struct, butbuffer_length
field remains zero and the buffer is initially empty.memcpy
only writes data to the buffer (within capacity, so no problems here), butbuffer_length
remains untouched and sonew_msg.get_rcl_serialized_message()
is in logically invalid state which can't be properly interpreted bydeserialize_message
. So, before doing deserializing, you should fix this by manually settingbuffer_length
(this doesn't require reallocation since it's, trivially, within capacity to which it is equal):