Operator: __DELETE
The operator is an extension of the IEC 61131-3 standard.
Important
For compatibility, the compiler version must be >= 3.3.2.0.
The operator releases the memory of instances that the "__NEW" operator generated dynamically. The __DELETE operator does not have a return value and the operand is set to zero after this operation.
Requirement: The Use dynamic memory allocation option is selected on the Application Build Options tab in the properties dialog of the application.
__DELETE (<pointer>)
Important
Two tasks should not call __DELETE simultaneously. Either you use a semaphore (SysSemEnter) or comparable method to prevent any concurrent calling of __DELETE , or you use __DELETE in one tasks only (recommended).
You can use a semaphore (SysSemEnter) to prevent two tasks from allocating memory at the same time. As a consequence, the extensive use of __DELETE causes higher jitter.
If Pointer references a function block, then CODESYS calls the associated FB_EXIT method before the pointer is set to zero.
Function Block
FUNCTION_BLOCK FBDynamic VAR_INPUT in1, in2 : INT; END_VAR VAR_OUTPUT out : INT; END_VAR VAR test1 : INT := 1234; _inc : INT := 0; _dut : POINTER TO DUT; neu : BOOL; END_VAR out := in1 + in2;
Method (FB_Exit)
METHOD FB_Exit : BOOL VAR_INPUT bInCopyCode : BOOL; END_VAR __Delete(_dut);
Method (FB_Init)
METHOD FB_Init : BOOL
VAR_INPUT
        bInitRetains : BOOL;
        bInCopyCode : BOOL;
END_VAR
_dut := __NEW(DUT);
Method
METHOD INC : INT VAR_INPUT END_VAR _inc := _inc + 1; INC := _inc;
Program
PLC_PRG(PRG)
VAR
        pFB : POINTER TO FBDynamic;
        bInit: BOOL := TRUE;
        bDelete: BOOL;
        loc : INT;
END_VAR
IF (bInit) THEN
        pFB := __NEW(FBDynamic);
        bInit := FALSE;
END_IF
IF (pFB <> 0) THEN
        pFB^(in1 := 1, in2 := loc, out => loc);
        pFB^.INC();
END_IF
IF (bDelete) THEN
        __DELETE(pFB);
END_IF