As discussed above, objects are persistent, meaning they exist in memory until you destroy them. We can break the life of an object into three phases: creation and initialization, use, and destruction. Object lifecycle routines allow the creation and destruction of object references; lifecycle methods associated with an object allow you to control what happens when an object is created or destroyed.
This section will discuss the first and last phases of the object lifecycle; the remainder of this chapter discusses manipulation of existing objects and use of object method routines.
Object references are created using one of two lifecycle routines: OBJ_NEW or OBJARR. Newly created objects are initialized upon creation in two ways:
An object's lifecycle method INIT is a function named Class::INIT (where Class is the actual name of the class). The purpose of the INIT method is to populate a newly-created object with instance data. INIT should return a scalar TRUE value (such as 1) if the initialization is successful, and FALSE (such as 0) if the initialization fails.
The INIT method is unusual in that it cannot be called outside an object-creation operation. This means that-unlike most object methods-you cannot call the INIT method on an object directly. You can, however, call an object's INIT method from within the INIT method of a subclass of that object. This allows you to specify parameters used by the superclass' INIT method along with those used by the INIT method of the object being created. In practice, this is often done using the _EXTRA keyword. SeeKeyword Inheritance for details.
Use the OBJ_NEW function to create an object reference to a new object heap variable. If you supply the name of a class structure as its argument, OBJ_NEW creates a new object containing an instance of that class structure. Note that the fields of the newly-created object's instance data structure will all be empty. For example, the command:
obj1 = OBJ_NEW('ClassName')
creates a new object heap variable that contains an instance of the class structure ClassName, and places an object reference to this heap variable in obj1. If you do not supply an argument, the newly-created object will be a null object.
When creating an object from a class structure, OBJ_NEW goes through the following steps:
| Note |
See OBJ_NEW for further details.
Use the OBJARR function to create an array of objects of up to eight dimensions. Every element of the array created by OBJARR is set to the null object. For example, the following command creates a 3 by 3 element object reference array with each element contain the null object reference:
obj2 = OBJARR(3, 3)
See OBJARR for further details.
Use the OBJ_DESTROY procedure to destroy an object. If the object's class, or one of its superclasses, supplies a procedure method named CLEANUP, that method is called, and all arguments and keywords passed by the user are passed to it. The CLEANUP method should perform any required cleanup on the object and return. Whether a CLEANUP method actually exists or not, IDL will destroy the heap variable representing the object and return.
The CLEANUP method is unusual in that it cannot be called outside an object-destruction operation. This means that-unlike most object methods-you cannot call the CLEANUP method on an object directly. You can, however, call an object's CLEANUP method from within the CLEANUP method of a subclass of that object.
Note that the object references themselves are not destroyed. Object references that refer to nonexistent object heap variables are known as dangling references, and are discussed in more detail in Dangling References.
See OBJ_DESTROY for further details.