__DELETE

The operator is an extension of the IEC 61131-3 standard.

The operator releases the memory of instances, which the operator __NEW generated dynamically. The operator DELETE has no return value, and the operand is set to 0 after this operation.

Syntax: __DELETE (<Pointer>)

If a pointer points to a function block, TwinCAT calls the corresponding method FB_exit before the pointer is set to 0.

__DELETE 1:

Dealing with dynamic memory

Always specify the exact type of the instance whose memory is to be released.
For inheritance, specify the derived function block (variable of type POINTER TO FB_Sub), not the basic function block (variable of type POINTER TO FB_Base).
If the basic function block does not implement an FB_exit function, FB_exit is not called if __DELETE (pfbBase) is called later!

Sample:

Function block FB_Dynamic:

FUNCTION_BLOCK FB_Dynamic 
VAR_INPUT 
    nIn1, nIn2 : INT; 
END_VAR 
VAR_OUTPUT 
    nOut : INT; 
END_VAR 
VAR
    nTest1 : INT := 1234; 
    _inc   : INT := 0; 
    _dut   : POINTER TO DUT; 
    bNeu   : BOOL; 
END_VAR
nOut := nIn1 + nIn2;

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 INC:

METHOD INC : INT 
VAR_INPUT 
END_VAR 
_inc := _inc + 1; 
INC  := _inc; 

Program MAIN:

PROGRAM MAIN 
VAR 
    pFB     : POINTER TO FB_Dynamic; 
    bInit   : BOOL := TRUE; 
    bDelete : BOOL; 
    nLoc    : INT; 
END_VAR
IF (bInit) THEN 
    pFB := __NEW(FB_Dynamic);
    bInit := FALSE; 
END_IF

IF (pFB <> 0) THEN
    pFB^(nIn1 := 1, nIn2 := nLoc, nOut => nLoc); 
    pFB^.INC(); 
END_IF 

IF (bDelete) THEN 
    __DELETE(pFB); 
END_IF