Modern C++ API - dds::core::xtypes::DynamicData

2 posts / 0 new
Last post
Last seen: 4 years 3 months ago
Joined: 08/25/2015
Posts: 32
Modern C++ API - dds::core::xtypes::DynamicData


it looks like dds::core::xtypes::DynamicData does not hold a copy of it's dds::core::xtypes::DynamicType which it was constructed with. This results in an exception when trying to access DynamicData.type() after the DynamicType instance get out-of-scope. In practice this does not occur as long as we're using rti::topic::dynamic_type<T>::get() (it returns a reference to a static object) to construct the DynamicData but when we:

- want to discover Types on runtime with dds::topic::PublicationBuiltinTopicData/dds::topic::SubscriptionBuiltinTopicData

- create our own DynamicType (e.g. as shown in online help, "Create a DynamicType by instantiating it and adding members")

Here the example:

dds::core::xtypes::StructType create_mytype() // from moden C++ API documentation
  using namespace dds::core::xtypes;
  // First, let's create Foo. We're using an initializer_list of Members
  StructType foo(
        "Foo", {
          Member("x", primitive_type<int32_t>()).key(true),
          Member("y", primitive_type<int32_t>())
  // Create MyType and add members
  StructType mytype("MyType");
  mytype.add_member(Member("my_long", primitive_type<int32_t>()));
  mytype.add_member(Member("my_string", StringType(512)));
  mytype.add_member(Member("my_foo", foo));
  mytype.add_member(Member("my_sequence", SequenceType(primitive_type<int32_t>(), 10)));
  mytype.add_member(Member("my_array", ArrayType(foo, 5)));
  mytype.add_member(Member("my_optional", foo).optional(true));
  return mytype;

dds::core::xtypes::DynamicData get_data()
  dds::core::xtypes::StructType type = create_mytype();
  dds::core::xtypes::DynamicData data(type);
  fprintf(stdout, "type: %p, data: %p, data.type(): %p\n", &type, &data, &data.type());
  return data;

void ddsTest()
  dds::core::xtypes::DynamicData data = get_data();
  fprintf(stdout, "data: %p, data.type(): %p\n", &data, &data.type());
  try {
    fprintf(stdout, "data.type().name(): %s\n", data.type().name().c_str());
  } catch (const std::exception &ex)
    fprintf(stderr, "Got exception: %s\n", ex.what());

I can't find any documented restriction in using DynamicType in combination with DynamicData. What's the recommended practice to handle such usecases?

Last seen: 2 months 2 weeks ago
Joined: 04/02/2013
Posts: 195

I have confirmed that this is a bug. As you say, the DynamicData constructor is not making a copy of the DynamicType.

I will make sure it's fixed in the next release.

Thank you for reporting it.