Reference Counting

An object that is managed using a handle keeps track of all the places where a reference to it is stored. When the number of references to the object drops to 0, it is deleted. The object stays alive as long as there are still references to it. The number of references to an object is known as its reference count.

The reference count is:

  • Set to 1 when it is created.
  • Increased by 1 whenever another handle references it.
  • Decreased by 1 whenever a handle that referenced it is assigned another value, or whenever the handle variable goes out of scope.

For example, consider handles to a component instance:

variables
  handle vHandle1, vHandle2
endvariables

newinstance "myComponent", vHandle1 callout 1
vHandle2 = vHandle1 callout 2
vHandle1 = 0  callout 3
vHandle2 = 0  callout 4
  1.  Reference count = 1; component instance object is created, returning 1 handle
  2. Reference count = 2; component instance handle is copied to another handle
  3.  Reference count = 1; reference count of first handle is set to 0
  4.  Reference count = 0; there are no longer any handles the reference the object, so it is deleted

Note:  When you use handles, you do not have to use the deleteinstance statement to remove instances. This is done automatically by Uniface.