How to properly zero-initialize a generated type (traditional C++)
The problem appears to be sequence's default constructor is marked explicit.
What I've tried:
- Type t;
This leaves fields uninitialized, it's what I'm trying to avoid.
- Type t{};
This *should* be the solution, but it does not compile (clang, mac) due to the sequence's explicit default constructor.
- Type t; std::memset(&t, 0, sizeof(t));
This is undefined behavior because std::is_trivialy_copyable::value == false.
- Type t; TypeSupport::initialize_data(t);
This does not zero anything. In fact, if the pointer for the string is a junk value, it will attempt to dereference it resulting in UB.
IDL:
struct NestedType{
long dummy;
}; //@top-level false
struct Type {
short id; //@key
string<256> value;
sequence more_values;
};
Hi,
TypeSupport::initialize_data should initialize the sample. It should set primitive types to zero, allocate memory for the string and the sequence, and make them empty.
If you review the generated code, this is the function that would initialize your 'Type' (I made the sequence a sequence of shorts for simplicity):
As you can see, it should allocate a new string.
Alex
The problem is I do not want to allocate memory, only to initialize the top-level, so I set allocParams to FALSE. The existing value for smple->value is junk (not NULL) which causes '\0' to get written to un-allcoated memory.
I am also doing this in generic/templated code. I can't set the fields directly, as I want my function to work for any generated type.
Anyway, I think I solved my problem: The initialization should be:
Type f = Type();
Related: the reason I'm trying to get a zerod-out, non-allocated object is to pass it to unregister_instance where I already have a valid instance handle. The docs for unregister_instance say the data may be NULL in this case, but the api is pass-by-reference (not pointer) so I have to give it somthing. It is crashing if there is junk in the data object passed.