Heap references and stack references
The objects referenced by references can be located in the heap or in the stack. Objects in the heap are instances of classes or are anonymous data objects. Objects in the stack are the global data objects of programs and the local data objects of procedures stacked there.
- Heap references point to objects or parts of objects in the heap
CREATE DATA) or by applying the statement
GET REFERENCEor the addition
REFERENCE INTOto data objects in the heap. Heap references are always memory-preserving, which means that, as long as a heap reference points to an object or a part of an object, then the object is not deleted by Garbage Collector.
- Stack references point to objects or parts of objects in the stack
GET REFERENCE, the reference operator
REF, or the addition REFERENCE INTO to data objects in the stack. Stack references are not memory-preserving.
A non-initial object reference always points to an instance of a class, which means it is always a heap reference. A data reference can be a heap reference or a stack reference:
- A data reference is a heap reference if it
- points to an instance attribute or a part of an instance attribute of an object,
- points to an anonymous data object or a part of an anonymous data object.
- A data reference that points to a data object or a part of a data object in the stack is a stack reference.
Heap references preserve the memory and cannot become invalid, with one exception: a heap reference
to a row in an internal table becomes invalid if the table row is deleted. Stack references also become
invalid if the referenced data object is removed from the stack (that is, if the associated procedure is ended). The latter also applies to the local data in instance methods. The
IS BOUND for an invalid reference is false.
- Field symbols have the same properties with respect to preserving memory and validity as data references. Field symbols to which objects or parts of objects are assigned in the heap are memory-preserving.
- Internal tables are dynamic
data objects and have a special role because they have their own memory management. They allocate
and release memory regardless of the statement
CREATEand Garbage Collector. This means that heap references to rows in internal tables can become invalid. This is not the case for strings, since no references to substrings of strings are allowed.
- A reference is classified as a heap reference or a stack reference depending on its value only, and not by the type of the reference variable in which it is stored. In particular, assignments between reference variables do not change the relevant properties of the copied references.