Uninitializing and Deleting Instances Using UNINIT

Object classes, while able to create instances, have no direct control over their deletion. If you assign a new value to a variable, REXX automatically reclaims the storage for the old value in a process called garbage collection.

If variables of other objects no longer reference an instance, REXX automatically reclaims that instance. If the instance has allocated other system resources, you must release them at this time using an UNINIT method. REXX cannot automatically release these resources because it is unaware that the instance has allocated them.

In the following example, the value passed to text is initialized by REXX using INIT and deleted by REXX using UNINIT. This program makes visible REXX's automatic invocation of INIT and UNINIT by revealing its processing on the screen using the SAY instruction:

/* UNINIT.CMD -- example of UNINIT processing */

a=.scratchpad~new('Of all the things I''ve lost')
a=.scratchpad~new('I miss my mind the most')
say 'Exiting program.'
exit

::class scratchpad

  ::method init
    expose text
    use arg text
    say 'Remembering' text

  ::method uninit
    expose text
    say 'Forgetting' text
    return

Circumstances dictate when uninitialization processing is needed--when a message object holds an unreported error that should be reported and cleared, for example. If an object requires uninitialization, define an UNINIT method to specify the processing you want.

If UNINIT is defined, REXX runs it before reclaiming the object's storage. If an instance has more than one UNINIT method (for example, UNINIT is defined in multiple classes), each UNINIT method is responsible for sending the UNINIT message up the hierarchy (using the SUPERCLASS overrides) to run the topmost version of INIT.


[Back: Returning String Data Using STRING]
[Next: Special Variables]