Why does an object reference saved by the trigger handler go bad and causes a crash?

From Support
Jump to: navigation, search

Question:

Why does an object reference saved by the trigger handler go bad and causes a crash?

Answer:

An application trigger handler method keeps a copy of the ObjLocation::Desc ref member for later use, but finds that using the reference leads to crashes. The reason is usually due to improper management of the local reference used at a later time by the handler or other application code.

The ObjLocation::Desc structure ref member is an Object_var.

After the trigger call to impl_is_ready() or impl_is_down() completes, the ObjLocation::Desc structure and the ref member within it are released.  So unless the application has taken precautions to duplicate the reference, subsequent access to the  stored reference may lead to a crash.

Here are two  safe approaches to making a copy of the  reference for later use. Either of these approaches should protect the refeence so that it will not be released before it is used and will not leak memory.

Approach #1: Use an Object_var, do not _duplicate

class TriggerHandlerImpl : public _sk_ObjLocation::_sk_TriggerHandler
{

 private:
  Object_var myObj;

 public:

...

  void impl_is_ready( const ObjLocation::Desc& desc ) {
        myObj = desc.ref ;
  }

...

Approach #2: Use an Object_ptr, do  _duplicate, but don't forget to release

class TriggerHandlerImpl : public _sk_ObjLocation::_sk_TriggerHandler
{

 private:
    Object_ptr myObj;

 public:

    ~TriggerHandlerImpl(  ) {
          // assuming that this is the appropriate place to release the ptr
          CORBA::release(myObj);
       };

...

  void impl_is_ready( const ObjLocation::Desc& desc ) {
        myObj = CORBA::Object::_duplicate( desc.ref );
  }

...



Article originally contributed by