Created On:  19 April 2011

Problem:

  • Product Name: VisiBroker
  • Product Version: All
  • Product Component: C
  • Platform/OS Version: All
Object references in C are dynamically allocated and needed to be taken to avoid leaking the associated memory. CORBA data, object reference, can be either dumb (_ptr) or smart (_var) pointer. And they are usually mixed together in the same section of code. Understanding the assignment behaviour with these 2 pointers will help to avoid memory leaking.

Resolution:

OBJECT_ptr      : A dumb pointer representation of the object reference which has the same semantics of ‘OBJECT *’, but is preferred because of standards compliance and portability. Hence using dumb pointer, you will need to take care of memory allocated.

OBJECT_var     : A smart pointer representation of the object reference, and is safe from memory leaks by automatically deleting its reference memory when necessary.

 

1.       Using _ptr types

{
  // Initialize a first pointer’s reference, ref-count=1
  Bank_ptr firstP = /* Bank reference */;
  {
     // Duplicate a dumb pointer, ref-count=2
     Bank_ptr secondP = Bank::_duplicate(firstP);
     ...

     // Must release before exit the scope, ref-count=1
     CORBA::release(secondP);
  }
  ...

  // Release first pointer, ref-count=0, memory released
  CORBA::release(firstP);
}

Note: Never call ‘delete’ on an object reference, as in ref-count implementation, this will raise the chance of dangling pointer. And the program will crash upon attempt made to dereference one of these dangling pointers.

 

2.       Using _var types:

{
  // Initialize pointers to refer to 2 distinct object references 
  // with ref-count=1
  Bank_var firstV = /* Initialize Bank reference to ‘obj1’ */;
  Bank_var secondV =/* Initialize Bank reference to ‘obj2’ */;

  second = firstV;

  // The assignment, will release the ref-count of obj2 to 0
  // and release the memory allocated to ‘obj2’.
  // Then increase the ref-count of obj1 to 2     
}

When it exits the scope, the 2 smart pointers will be released and one by one reduces the ref-count of ‘obj1’ to 0, the memory located will be released, no leak observed.

 

3.       Using mixed types:

a.      Assignment of _var = _ptr

{
  // Initialize pointers to refer to 2 distinct object references
  //with ref-count=1
  Bank_ptr dumbP = /* Initialize Bank reference to ‘obj1’ */
  Bank_var smartV = /* Initialize Bank reference to ‘obj2’ */
 
  // Assign _var to _ptr
  smartV = dumbP;

  // After the assignment, the obj2’s ref-count reduced to 0 and its 
  // memory allocated released.
  // The smart pointer smartV assume the ownership of ‘obj1’,
  // and obj1’s ref-count is still 1
}

Note: You don’t really need CORBA::release(dumbP), as smartV will be destructed when exiting out of the scope, and the memory allocated for ‘obj1’ will be released as well.

 

b.      Assignment of _ptr = _var

{
  // Initialize pointers to refer to 2 distinct object references
  //with ref-count=1
  Bank_ptr dumbP = /* Initialize Bank reference to ‘obj1’ */
  Bank_var smartV = /* Initialize Bank reference to ‘obj2’ */

  // Must release the memory located by ‘dumbP’ before assignment
  CORBA::release(dumbP);

  // Assign _ptr to _var
  dumbP = smartV;

  // After the assignment, the both pointers refer to ‘obj2’,
  // but its ref-count still remains 1
}

Similarly, after exiting out of the scope, the ‘smartV’ pointer will be destructed, and subsequently releasing the memory allocated to ‘obj2’.