[omniORB] any & objrefs.
Sai-Lai Lo
S.Lo@uk.research.att.com
07 Jul 1999 12:23:06 +0100
Renzo,
IMO, the spec. before 2.3 is very vague about this issue of ownership and
it is only in 2.3 that the whole issue has been clarified.
Insertion:
----------
It has always been the case in omniORB2 that there are 2 insertion
operators. For an interface A:
1) void operator>>=(CORBA::Any& a, T_ptr& t)
The object reference is copied (or duplicated). So the following is the
correct usage:
CORBA::Any a;
A_ptr v = ...;
a <<= v; // copy insertion
CORBA::release(v); // we still own v
2) void operator>>=(CORBA::Any& a, T_ptr*);
The object reference is not copied. The following is the correct usage:
CORBA::Any a;
A_ptr v = ...;
a <<= &v; // a now owns v, no need to release v ourselves
Shock horror! I just noticed that the 2) operator is missing from the
omniORB 2.8.0pre1 stubs!!! This will be corrected.
The semantics of these operators has not changed in omniORB 2.8.0.
Extraction:
----------
This is where the semantics has changed.
CORBA::Boolean operator>>=(const CORBA::Any&, A_ptr&);
Before 2.8.0, the following is correct usage:
CORBA::Any a;
... // a contains a A_ptr
A_ptr v;
a >>= v;
... // Use v
CORBA::release(v); // have to release v because the extraction
// operator copy the object reference
>From 2.8.0 onwards, the following is correct usage:
CORBA::Any a;
... // a contains a A_ptr
A_ptr v;
a >>= v;
... // Use v
// No need to release the object reference in v but make sure that
// the Any a is still in scope while you use v.
As you can see, code that use Any in the old way would break in 2.8.0
because the object reference would been freed more that once. To smooth the
transition, do the following in your code and the ORB would revert to the
old usage:
omniORB::omniORB_27_CompatibleAnyExtraction = 1;
------
One last point, widening extraction of reference is always (2.8.0 or
earlier releases) copied:
CORBA::Any a;
... // a contains a A_ptr
CORBA::Object_ptr obj;
a >>= CORBA::Any::to_object(obj); // Extract as CORBA::Object_ptr
...
CORBA::release(obj); // Has to release the object reference because
// its a copy.
Sai-Lai
>>>>> Renzo Tomaselli writes:
> I would need a little explanation about object refs management with anys.
> Up to 2.6.1 the manual stated that any values did not manage refs; indeed the refcount did not change across insertion/extraction. This created some troubles in cases when ownership of refs could not be transferred to any. I wrote a small portable package to fully serialize anys through dynany recursive scan (and the contrary); when loading from a stream to an any, there was need to keep track of all refs created through object_to_string() so that they could be released upon context deletion (e.g. StreamToAny object disposal).
> Now, if I correctly understand 2.8 semantics, anys do a _duplicate() when they are fed with refs, and all extractors should do the same (so this would make the above problem much simpler because I can _release() the ref after the any has taken it, and the ref is then disposed when the any is destroyed unless someone _duplicate() the ref).
> Is that correct ? Thanks,
--
Sai-Lai Lo S.Lo@uk.research.att.com
AT&T Laboratories Cambridge WWW: http://www.uk.research.att.com
24a Trumpington Street Tel: +44 1223 343000
Cambridge CB2 1QA Fax: +44 1223 313542
ENGLAND