[Enterprise Extensions only]

CORBA programming: Storage management and _var types

The C++ bindings try to make the programmer's storage management responsibility as easy as possible. One aspect of this is the "_var" types. For each user-defined structured IDL type T (struct, union, sequences, and arrays) and for interfaces, the bindings generate both a class T and a class T_var. The classes CORBA::String and CORBA::Any also have corresponding CORBA::String_var and CORBA::Any_var classes.

The essential purpose of a _var object is to hold a pointer to dynamically allocated memory. A _var object can be used as if it were a pointer to the IDL type for which it is named; special constructors, assignment operators, and conversion operators make this work in a way that is invisible to programmers. The memory pointed to by a _var object is always considered to be owned (managed) by the _var object, and when the _var object is deleted, goes out of scope, or is assigned a new value, it deletes (or, in the case of an object reference, releases) the managed memory.

A typical _var object is declared by a programmer as an automatic (stack) variable within a code block, and is then used to receive an operation result or is passed to an operation as an out parameter. Later, when the code block is exited, the _var object destructor runs and its managed memory is deleted (or, for object references, released).

When a pointer (rather than another _var object or struct/union/array/sequence element) is assigned to (or used to construct) a _var object, this pointer should point to dynamically allocated memory, because the _var object does not make a copy; it assumes ownership of the pointer and will later delete it (or, for object references, release it). The single exception is that pointers to const data can be assigned to a _var object. When this occurs, the _var object dynamically allocates new memory and copies the const data into the new memory. A pointer assigned to a _var object must not be "owned" by some other data structure, and the pointer should not be subsequently used by the application except by the _var object.

The default constructor for a _var type loads the contained pointer with NULL. You must assign a value to a _var object created by a default constructor before invoking methods on it, just as you must assign a value to a pointer variable before invoking methods on it.

The copy constructor and _var assignment operator of a _var type perform a deep copy of the source data. The copy is later deallocated (or released, in the case of object references) when the _var is destroyed or when it is assigned a new value.

The following is the typical form for a T_var class, emitted for an IDL--constructed data type named T:

class T_var 
{
   public:
   T_var ();
   T_var (T*);
   T_var (const T_var&);
   ~T_var ();
   T_var  operator= (T*);
   T_var  operator= (const T_var&);
   T * operator-> const ();
   ...
};

For more information on storage management and argument passing see Argument passing considerations for C++ bindings.