[omniORB] strange behavior of '_this()' method

Michael omniorb at bindone.de
Wed Apr 1 10:01:44 BST 2009


Michael Kilburn wrote:
> On Tue, Mar 31, 2009 at 4:59 AM, Michael <omniorb at bindone.de> wrote:
> 
>>  PortableServer::Servant servant = _poa->reference_to_servant(obj);
>>                servant->_remove_ref();
>>
> 
> Just being pedantic... this is better:
> 
> PortableServer::Servant_var servant = _poa->reference_to_servant(obj);
> 

Sorry, but no :) PortableServer::Servant is a pointer to (not a copy of)
 the incarnating servant in the active object map. You don't want the
_var class to clean that up ever (and btw it won't compile anyway
because PortableServer::Servant is a local corba pseudo class thingy).
You're confused about memory management rules here (which brings me back
to my previous argument about CORBA complexity and developers getting
confused about it :).

So you understand the context:
try
{
  PortableServer::Servant servant = _poa->reference_to_servant(obj);
  servant->_remove_ref();
  IRObject_impl* impl = dynamic_cast<IRObject_impl*>(servant);
  if (impl)
  {
    // do something
  }
  // there is no delete statement here, try this to crash it:
  // delete servant;
}
catch(...)
{
// damn
}

This code does not lose any memory(!)
Note that reference_to_servant returns a pointer to a local servant
object, not a CORBA object reference (the servant returned could be also
a default servant (not sure if you ever worked with those) so you will
get a pointer to the default servant for every obj passed (because all
objects are backed by the same servant). That's why it is called
reference_to_servant :).




More information about the omniORB-list mailing list