An Interface Definition Language (IDL) struct type is mapped to a corresponding C++ struct whose field names correspond to those in the IDL declaration and whose field types support access and storage of the C++ types corresponding to the IDL struct field types. Dynamically allocated storage used to hold this type of C++ struct must be allocated and freed using the C++ new and delete operators.
When a struct is constructed, the default constructor for each field is invoked, object reference files are initialized to nil references, and string fields are initialized to an empty string. When a struct is deleted (or goes out of scope), the destructor for each field is invoked. All of the object references are released and all of the strings are freed. The copy constructor performs a deep copy including duplicating object references. The assignment operator acts as a destructor (releasing all memory) followed by a copy constructor.
When assigning a value to a struct field that is an object reference, the assignment operator for the struct field automatically releases the previous value, if any. When assigning an object reference pointer to a struct member, the struct member assumes ownership of the pointer (no _duplicate is made) and the application must no longer access the pointer directly. If this is not the desired behavior, then the caller can explicitly _duplicate the object reference before assigning it to the struct member. However, when assigning to an object reference struct member from a _var object or from another struct, union, array, or sequence member (rather than from an object reference pointer), a _duplicate is made automatically.
When assigning a value to a struct field that is a string or when the struct is deleted or goes out of scope, any previously held (non-null) string is automatically freed. Assigning a char* to a string field does not make a copy, but assigning a const char *, a String_var, or another struct, union, array, sequence string member does make a copy automatically. Never assign a string literal (for example, "abc") to a string struct member without an explicit cast to "const char*". When assigning a char* that occupies static storage (rather than one that was dynamically allocated), the caller can use CORBA::string_dup to duplicate the string before assigning it.
As with all constructed types, a _var type is provided for managing an instance of the C++ struct that corresponds to an IDL struct. When assigning one struct's _var to another, the receiving _var deletes its current pointer (thus running all contained destructors) and creates a new struct to hold the assignment result. The new struct is initialized using copy constructors for each of the contained fields. For example, if the source struct has an object reference field, the struct _var assignment automatically duplicates this reference.
The IDL that follows is used in the succeeding example, which shows both correct and incorrect ways to create and manipulate the corresponding C++ struct and the corresponding _var type :
Interface A { struct S { string f1; A f2; }; };
The following code illustrates both correct and incorrect ways to create and manipulate the corresponding C++ struct and the corresponding _var type.
{ A::S_var sv1 = new A::S; A::S_var sv2 = new A::S; // sv1->f1 = "abc"; -- Wrong! f1 cannot free this pointer later sv1->f1 = CORBA::string_alloc(20); A_ptr a1 = // get an A somehow A_ptr a2 = // get an A somehow sv1->f2 = a1; // a1 still has ref cnt = 1 sv2->f1 = CORBA::string_alloc(20); sv2->f2 = a2; // a2 still has ref cnt = 1 sv1 = sv2; // This runs copy ctors, and increments a2's ref cnt. // Also, a1's ref count is decremented. sv1->f1 = sv2->f1; }