[omniORB] memory management

Stefan Seefeld seefelds@MAGELLAN.UMontreal.CA
Fri, 22 Dec 2000 10:31:22 -0500


"ERIKSSON,TOBIAS (A-Sweden,ex1)" wrote:
> 
> Hi
>  I continously get a memory failure, SIGSEGV, but why?
> 
> What I have is basically this:
> 
>   SClass_impl *a1 = new SClass_impl();
>   IClass_impl *a2 = new IClass_impl();
> 
>   //sequece seqv
> 
>   seqv.length( 2 );
>   seqv[0] = SClass::_duplicate( a1->_this() );
>   seqv[1] = IClass::_duplicate( a2->_this() );

_this() already returns a new reference. Calling _duplicate as well increases
the proxy ref counter by two. Beside, _this() implicitely activates the servants,
resulting in the servant's ref counters to be two as well...

>   a1->dosomething();
>   a2->domore();
> 
>   a1->_remove_ref();
>   a2->_remove_ref();

now the servant ref counters are both set to one.

>   orb->destroy();

> When the program is about to shut down, it executes the _remove_ref() lines.
> I have a line in the destructor of SClass and IClass that prints that it is
> beeing executed. But then the program dies a SIGSEGV is received.

assuming that you indeed derive from PortableServer::RefCountServant the
destructor will be called when the POA - being destroyed - deactivates all
active objects, and calls _remove_ref(). Since you (correctly) remove one
ref count yourself, that will do the right thing, i.e. the POA assumes ownership.

> The seqv[0] is treated as _var and so when _duplicate() is called it will
> increase the reference counting, and when it reaches the _remove_ref() it
> will decrease the reference counting which now should be 1 again, but
> something is not right here, cause as the sequence is deleted in the end it
> will call CORBA::release( obj ) on the objects in the list (according to the
> book I have) and thus the reference counting would now be 0, and the object
> deleted. The line in my destructor is printed, but the application fails
> with a SIGSEGV in the orb->destroy().

I think you are mixing servants, objects, and proxies. 

* the _duplicate/CORBA::release pair affects the proxy classes only.

* _add_ref() / _remove_ref() affects the servants (only if you derive from
  PortableServer::RefCountServant

* poa->activate_object() / poa->deactivate_object() affects the objects

the first is fully unrelated to the other two. The last two are related through
the POA calling _remove_ref() on the servant after deactivation as soon as there
are no pending requests for the object in question. In other words, using
PortableServer::RefCountServant is a nice way to get rid of servant memory management,
as the servant lifecycle is simply bound to the object lifecycle.

> If someone has a comment on this that would be great, I'll read it when I
> get back after new years eve.

I hope this will clear some of the confusion, though it is not yet clear from
the code snippets you sent where the error is (the error I pointed out should
leak memory, not double delete. I.e. the segfault comes from a different place.)

> Merry Christmas and a happy new year

Same to you, thanks

Regards,	Stefan
_______________________________________________________              
              
Stefan Seefeld
Departement de Physique
Universite de Montreal
email: seefelds@magellan.umontreal.ca

_______________________________________________________

      ...ich hab' noch einen Koffer in Berlin...