Programming CORBA with Orbacus on Linux
The _var classes
The _var classes generated by the IDL compiler helps you by wrapping some
memory management functions around the raw _ptr pointer classes. If a _var
reference goes out of scope, the reference counter of the underlying server object is
decremented and the object is eventually destroyed. So the manual calls to CORBA::release
can be avoided. This responsibility for memory management is sometimes called "ownership" of
the reference. However, there remains some rules concerning the reference counting.
Declaration and initializtaion
-
The declaration of a _var variable creates a nil reference:
X_var xv; // is_nil(xv)==true
-
The variable can be initialized by a _ptr reference:
X_ptr xp = <get reference from somewhere>
X_var xv(xp);
In this case, the variable takes ownership of the reference (does not increment the
reference counter) and gets responsibility for releasing the reference. After xv
is gone out of scope, also xp is invalid!
-
The initialization of a _var reference with another _var variable:
X_var xv = <get reference from somewhere>
X_var xv1(xv);
performs a deep copy, incrementing the reference counter of the server object. If one or
both of the variables goes out of scope, the correct release of the references automatically
takes place.
Assignments of var references
The assignment of _var references in the following way:
X_var xv = <get reference from somewhere>
X_var xv1 = <get another reference from somewhere>
xv1 = xv;
performs a deep copy of the server object, meaning the method X::_duplicate is
called and therefore the reference counter of the server object is incremented. Before
that, xv1 releases the reference it currently holds by decrementing the reference
counter of its original server object. In sum, all references of xv1 are properly
released and the new references are properly created. Both variables point to the same
server object, but can independently be used and going out of scope with proper
behaviour.
back to CORBA page
|