User Tools

Site Tools


dispose_de_allocate_heap_memory_.htm
Navigation:  Language Reference > 3 - Variable Declarations > Data Declarations and Memory Allocation >====== DISPOSE (de-allocate heap memory) ====== Previous pageReturn to chapter overviewNext page

DISPOSE( reference )

blk2blue.jpg

DISPOSE De-allocates heap memory previously allocated by a NEW statement.
reference The label of a reference variable previously used in a reference assignment with the NEW statement. This reference may be NULL and no ill effects will occur.

The DISPOSE statement de-allocates the heap memory previously allocated by a NEW statement. If DISPOSE is not called, the memory is not returned to the operating system for re-use (creating a “memory leak”). However, if you DISPOSE of a reference that is still in use (such as a QUEUE being displayed in a LIST control) you will quite likely cause a GPF that will be very difficult to track down.

DISPOSE(SELF) is a legal statement to de-allocate the current object instance. However, if used, it must be the last statement in the procedure, or any following references to the object will cause problems.

There is a way to pass a &STRING reference to a procedure in a way that it can be disposed in that procedure. Although a *STRING cannot be disposed, consider the following code:

MyProc  PROCEDURE (*STRING S)

Ref &STRING,AUTO

 CODE

 Ref &= S

 DISPOSE (Ref)

The Parameter/result of *STRING type can be considered as a read only &STRING value: you can change the string it points to but you can't set it to reference to another string. DISPOSE sets the reference passed to it as a parameter to NULL and hence *STRING parameters and

results can't be DISPOSEd directly but assigning their reference to a reference variable gives a solution. This is also true for any other data type.

If for any reason you plan to use DISPOSE on an object in a queue that was created with NEW, to prevent a possible memeory leak you must issue the DISPOSE prior to any CLEAR (or DELETE) is executed.

NewCNet.jpg

In WIN 32 Clarion (e.g., Clarion 6/7) DISPOSE is used to explicitly de-allocate memory allocated by NEW statement. In Clarion .NET DISPOSE doesn't free memory, but the destructor (DESTRUCT/Dispose method) of the corresponding class that is executed.

Example:

StringRef &STRING         !A reference to any STRING variable

Animal CLASS,TYPE

Feed    PROCEDURE(short amount),VIRTUAL

Weight  LONG

      END

AnimalRef &Animal         !A reference to any Animal CLASS

NameQ  QUEUE

Name    STRING(30)

      END

QueRef  &NameQ            !A reference to any QUEUE with only a STRING(30)

CODE

AnimalRef &= NEW(Animal) !Create new instance of an Animal class

DISPOSE(AnimalRef)       !De-allocate the Animal

QueRef &= NEW(NameQ)     !Create new instance of a NameQ QUEUE

DISPOSE(QueRef)          !De-allocate the queue

StringRef &= NEW(STRING(50())  !Create new STRING(50) variable

DISPOSE(StringRef)       !De-allocate the STRING(50)

See Also:

NEW

CLEAR

DELETE

dispose_de_allocate_heap_memory_.htm.txt · Last modified: 2021/04/15 15:57 by 127.0.0.1