Search code examples
data-distribution-service

Can a default constructed DDS topic type be published?


I am using OpenSplice DDS 6.4 OSS edition with C++ (the C++11 bindings). If I try to default-construct a topic instance and send it, perhaps modifying some of its fields, I get access violation exceptions somewhere in the guts of the writer. Is this a bug, or is it intended behaviour that the default constructed object is not valid?

The IDL I am using has a lot of unions in it, which I have a felling may be relevant.


Solution

  • The IDL to C++ language mapping says the following about the default initialization of unions

    The default union constructor performs no application-visible initialization of the union. It does not initialize the discriminator, nor does it initialize any union members to a state useful to an application. (The implementation of the default constructor can do whatever type of initialization it wants to, but such initialization is implementation-dependent. No compliant application can count on a union ever being properly initialized by the default constructor alone.)

    So it is not safe to construct a default initialized topic instance and send it on the wire.

    Just as reference, the IDL to C++11 language mapping says the following

    The default union constructor initializes the union. If there is a default case specified, the union is initialized to this default case. In case the union has an implicit default member it is initialized to that case. In all other cases it is initialized as empty. Assigning, copying, moving, and the destruction of default-constructed unions are safe.