Programming CORBA with Orbacus on Linux
The _ptr classes
The IDL compiler creates a _ptr class for each interface described in the IDL file.
There are several things to know for proper handling of these references.
The general usage of the class is as follows:
X_ptr xp = <get reference from somewhere>
xp->f();
CORBA::release(xp);
You get a reference to a server object from somewhere (an initial reference, a factory ...)
and can immediately use the object via this reference. Note that the server object's reference
count is normally incremented by getting a reference to X or invoking the factory for X.
Therefore, you have to release the reference by a call to CORBA::release after
using the object. (If you get the reference without incrementing the reference counter, you
are not expected to release the reference, but it's still possible or desired to do so.)
Copying of pointer references
Sometimes you want to get a real copy of a server object. You do this with a static method of
the server class:
X_ptr X::_duplicate(X_ptr)
This method creates a deep copy of the server object and increments the reference
counter. This means you will have to release the reference to the copy!
The typical usage of this method is the following:
X_ptr xp = <get reference from somewhere>
X_ptr xp1 = X::_duplicate(xp)
xp->f(); // call a remote method
xp1->f(); // equivalent
CORBA::release(xp); // release original object
CORBA::release(xp1); // release copy also
With an initial reference to a server object, you create a copy of it, incrementing the
reference counter. You can now use both references to invoke remote methods. After usage,
you will have to release both references to avoid leaks!
Assignments of pointer references
The assignment of a _ptr reference to another variable of the _ptr
type does not increment the reference counter, but performs a shallow copy instead
(both references points to the same object):
X_ptr xp = <get reference from somewhere>
X_ptr xp1 = xp;
xp->f(); // call a remote method
xp1->f(); // equivalent
CORBA::release(xp); // release original object,
// xp1 now dangles!!!
Due to this semantics, you must not release both references, because after releasing one of
the two identical references, the object is destroyed and the other reference dangles!
back to CORBA page
|