Skip to content

Dynamic Message Deserialization #2260

@wew84

Description

@wew84

Hi,

I'm relatively new to the rclcpp client after several years of using rclpy and both ROS1 clients. I'm trying to deserialize a message dynamically at runtime. I have the following callback for a generic subscriber:

[this, topic_name, topic_type](const std::shared_ptr<rclcpp::SerializedMessage> &message) {
    std::shared_ptr<rcpputils::SharedLibrary> library = rclcpp::get_typesupport_library(topic_type, "rosidl_typesupport_cpp");
    const rosidl_message_type_support_t * message_type_support = rclcpp::get_typesupport_handle(topic_type, "rosidl_typesupport_cpp", *library);
    auto members = static_cast<const rosidl_typesupport_introspection_cpp::MessageMembers *>(message_type_support->data);
    rcutils_allocator_t allocator = rcutils_get_default_allocator();
    auto data = allocator.allocate(members->size_of_, allocator.state);
    if (nullptr == data) {
        RCLCPP_ERROR(this->get_logger(), "Could not allocate message memory %zu", members->size_of_);
        return;
    }
    rclcpp::SerializationBase ser(message_type_support);
    ser.deserialize_message(message.get(), data);
...

When I run this, I get a members->size_of_ of 140737332973024, so I assume the casting to message members is incorrect.
I also tried the init_function() in members.

I may be misunderstanding the mechanism for introspection or the serialization API. Any help would be great. Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions