Try OpenEdge Now
skip to main content
Programming Interfaces
External Program Interfaces : Shared Library and DLL Support : Passing parameters to a shared library routine : Using MEMPTR variables as parameters : Freeing memory associated with a MEMPTR variable
 
Freeing memory associated with a MEMPTR variable
The region of memory associated with a MEMPTR variable remains allocated until it is freed. In some cases, the shared library routine frees the memory; in other cases, the calling procedure must free the memory using the SET-SIZE statement to set its size to zero (0). ABL cannot free the memory for you. It is up to you to ensure that the memory is freed, depending on the functionality of each shared library routine you use.
The following code fragment shows how to pass an INPUT-OUTPUT string value as a MEMPTR parameter in the case where ABL allocates the memory:
DEFINE VARIABLE cString AS CHARACTER NO-UNDO.
DEFINE VARIABLE iLength AS INTEGER   NO-UNDO.
DEFINE VARIABLE iSize   AS INTEGER   NO-UNDO.
DEFINE VARIABLE mVar    AS MEMPTR    NO-UNDO.

/* The longest string the DLL returns is 256 characters */
ASSIGN
  iLength            = LENGTH(cString)
  iSize              = (IF iLength > 256 THEN iLength ELSE 256)
  SET-SIZE(mVar)     = iSize
  PUT-STRING(mVar,1) = cString.

RUN DLLfunction (INPUT-OUTPUT mVar).

cString = GET-STRING(mVar, 1).

PROCEDURE DLLfunction EXTERNAL "anysystem.dll" ORDINAL 10:
DEFINE INPUT-OUTPUT PARAMETER StringParm AS MEMPTR NO-UNDO.
END PROCEDURE.
The DLL routine is the tenth function in the anysystem.dll file. The SET-SIZE statement allocates to mVar a memory region large enough to hold both the input and output cString values. The PUT-STRING statement stores the cString value in mVar. After passing mVar to the DLL routine, the GET-STRING statement returns the (new) value to cString.