AW: [omniORB] Destroy objects

Michael omniorb at bindone.de
Fri May 9 12:45:54 BST 2008


ZThread is a threading library, you could of course use any other locking mechanism (boost
threads, plain pthreads, omnithreads whatever).

Did your servant inherit from PortableServer::RefCountServantBase?

If it does, my example code is lacking a call to _remove_ref, so at the end of the destroy
function you should add:
_remove_ref()
(I'm sorry I forgot about that, the example I gave you is from a Servant Locator
implementation which handles this differently).

Assuming you created the object correctly, this should bring the reference count down to 1
(it is 2 before, because the poa increased it), after the destroy method has been left,
the orb will call remove_ref as well, bringing the reference count down to 0, which will
trigger the destructor to be called.

You can call _refcount_value() any time to check what the current reference count is. Also
 make sure you understand which calls increase the reference count (like activate does,
explictely or implicitely when calling _this() on an unactivated object)

cheers
michael

Silly & Chrischi wrote:
> Hello,
> 
> what do I have to include to use ZThread::RecursiveMutex?
> Andi t doesn't work. I tested your code send to me and I put in the destructor of my object the following:
> 
> Administrator_impl::~Administrator_impl(void) {
> 	cout << "destroy";
> 	delete mysql_;
> }
> 
> But I don't see the "destroy" in my console. And the next, if I set my object in the client to admin->destroy(); I can woork with this object later on, but I thought it doesn't exists anymore. So what do I do wrong?
> 
> Thanks.
> 
> -----Ursprüngliche Nachricht-----
> Von: Michael [mailto:omniorb at bindone.de] 
> Gesendet: Donnerstag, 24. April 2008 15:22
> An: christian83 at gmx.li
> Cc: omniorb-list at omniorb-support.com
> Betreff: Re: [omniORB] Destroy objects
> 
> Silly & Chrischi wrote:
>> Hello,
>>
>> I create an object with a factory-method. What have i had to call on the client, do destroy the object on the server? Every class has an destructor but I don't know what to call to start the destructor.
>>
>> Thank you.
>>
>>
>> _______________________________________________
>> omniORB-list mailing list
>> omniORB-list at omniorb-support.com
>> http://www.omniorb-support.com/mailman/listinfo/omniorb-list
> 
> You have to create an explicit destroy method on the object (also in IDL), that:
> - deactivates the object in the poa
> - deletes the object
> 
> (you might want to use RefCountServantBase and make destroy just decrement the reference
> count and set a deleted flag on the object)
> 
> Make sure you do not shoot yourself in multi threading scenarios.
> 
> Example:
> IDL:
> interface Xyz
> {
>   unsigned long getId();
>   void destroy();
> };
> 
> class Xyz_impl: virtual public POA_Test::Xyz, virtual public
> PortableServer::RefCountServantBase
> {
>   ZThread::RecursiveMutex _lock;
>   bool _destroyed;
>   PortableServer::POA_ptr _poa;
> 
> public:
> ...
>   CORBA::ULong getId()
>   {
>     ZThread::Guard<ZThread::RecursiveMutex> g(_lock);
>     if (_destroyed) throw CORBA::OBJECT_NOT_EXIST();
>     // do sth useful
>   }
> 
>   void destroy()
>   {
>     ZThread::Guard<ZThread::RecursiveMutex> g(_lock);
>     if (_destroyed) return; // might also want to throw here
>     // .. cleanup
>     _destroyed = true;
>     if (!_poa->non_existent())
>     {
>       PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
>       _poa->deactivate_object(oid);
>     }
>   }
> }
> 
> 
> Please note that this example is out of context and might not work 1:1 in your setup
> 
> 
> _______________________________________________
> omniORB-list mailing list
> omniORB-list at omniorb-support.com
> http://www.omniorb-support.com/mailman/listinfo/omniorb-list




More information about the omniORB-list mailing list