Managing memory for objects

LotusScript® automatically manages the memory associated with objects you create by tracking all references to the objects. LotusScript® also automatically frees the memory for objects by deleting them when no variables refer to the objects.

When you create an object, LotusScript® assigns a reference to the object and sets the object's reference count to 1. Whenever you assign an object reference for that object to a variable, LotusScript® increments the reference count by 1. When an object reference is no longer needed, such as when an object reference variable goes out of scope, LotusScript® decrements the object's reference count by 1. When the reference count reaches 0, no variables contain references to the object so LotusScript® automatically deletes the object and frees its memory.

In this example, LotusScript® deletes objects when the reference count returns to 0.

Class DemoObject
  Sub New
    Print "New" 
  End Sub
  Sub Delete
    Print "Delete" 
  End Sub

End Class

  Sub MyDemo
  ' localObject reference count is set to 1.
    Dim localObject As New demoObject
    If (Not (localObject Is NOTHING)) Then _
      Print "In MyDemo sub and localObject exists." 
  End Sub

Print "About to call MyDemo."
Call MyDemo
' Sub MyDemo is now out of scope...
' so the reference count is 0 and the object is deleted.
Print "Returned from MyDemo. Delete already ran."