Initialization and cleanup of objects

A pair of special methods called Initialize() and Destroy() may be defined on a class. These two methods provide a way to automatically perform some processing whenever an object of the class is created or destroyed, and may be optionally defined for any class.

The Initialize() method must be defined without any arguments. If it has been defined for a class, the Initialize() method is executed automatically when an object of that particular class is allocated, either when it is declared by a LOCAL or GLOBAL statement, or by a call to the EAlloc() intrinsic function. The Initialize() method is typically used for pre-setting data fields within the object to particular values. For example, the initialize method could be defined on the Circle class presented above to initialize a circle object to unit size:

define method Initialize() on Circle
  this.SetRadius(1.0)
enddefine

Now, whenever a circle is created, it will automatically have its radius pre-set to 1:

EASI>local Circle c
EASI>print c.Radius
1.0

If an object's initialization requires arguments to specify values for the initialization, this can only be accomplished by defining a separate initialization method (with a different name), and calling it explicitly for each object. If an object has a parent class, the Initialize() method of the parent class will also be executed, prior to the objects own Initialize() method.

Similarly, the Destroy() method must also be defined without any arguments. If it has been defined, it will be executed automatically when an object is de-allocated. The Destroy() method is typically used for freeing memory that was dynamically allocated by the object using the EAlloc() intrinsic, for closing files that the object may have opened, and for other such clean-up type activities. For objects that are allocated using a LOCAL declaration, its Destroy() method will be executed when the function in which it was declared terminates. For objects that are allocated dynamically using EAlloc(), the Destroy() method is called when they are deallocated by a corresponding call to EFree(). If an object has a parent class, the Note: A class may have only one parent class; multiple inheritance is not supported.

Destroy() method of the parent class will be executed also, after the objects own Destroy() method. Note however that if a function exits due to an EASI error, the Destroy() methods of local objects declared in the function will not be called.

© PCI Geomatics Enterprises, Inc.®, 2026. All rights reserved.