Tuesday, June 23, 2009

The C Destructor in Methods

A destructor is a special method which is called automatically when an object is deleted. Destructors can be used to release resources used by the object that are not included in the Garbage Collection. Currently ABAP Objects does not include a destructor, in which a regular ABAP processing block can be programmed.

For special cases and for internal use only, the predefined instance method destructor can be declared in the public visibility section of a class:

CLASS class DEFINITION.
PUBLIC SECTION.
METHODS destructor [NOT AT END OF MODE].
...
ENDCLASS.

In the implementation of the method destructor only one statement can currently be used:

CLASS class IMPLEMENTATION.
METHOD destructor.
SYSTEM-CALL c-destructor 'name' USING attr.
ENDMETHOD.
ENDCLASS.

This means that the destructor makes it possible to call a C routine name when an object is deleted. The routine must exist in the kernel in order that no syntax error occurs.

When the optional addition NOT AT END OF MODE is used, the destructor is not executed if the internal mode is closed anyway. Usually a destructor is also executed at the end of a mode and should mainly be used to release external resources involved that are not released automatically when the mode is closed.

When the C routine is called, an attribute attr of the class of any complex data type can be passed on to the routine. If several parameters are to be passed, an appropriate data type must be defined.

During the lifetime of an internal mode, the time when the method destructor is executed depends on when the Garbage Collector is started. When an internal mode is closed, the destructors that are not declared with the addition NOT AT END OF MODE are executed for all objects. In connection with inheritance, the destructors of the subclasses are executed before the destructors of the superclasses.

No comments:

Blog Archive