Pointer Checks (POU CheckPointer)

Monitoring function CheckPointer for pointers

Use this function to monitor the memory access of pointers during runtime. In contrast to other monitoring functions, there is no default implementation for CheckPointer. The user must carry out the implementation himself!

The purpose of the CheckPointer function is to check whether the transferred pointer points to a valid memory address, and whether the orientation of the referenced memory area matches the type of variable to which the pointer points. If both conditions are met, the pointer itself is returned. Otherwise, the function should perform proper error handling.

Pointer Checks (POU CheckPointer) 1:

Do not change the declaration part

To maintain the functionality of the monitoring functions, the declaration part must not be modified. The only exception is to add local variables.

Pointer Checks (POU CheckPointer) 2:

The monitoring function is not called implicitly for the THIS pointer and the SUPER pointer.

Pointer Checks (POU CheckPointer) 3:

The function CheckPointer also acts on variables of the type REFERENCE in the same way as on pointer variables.

Template

Declaration:

// Implicitly generated code : DO NOT EDIT
FUNCTION CheckPointer : POINTER TO BYTE 
VAR_INPUT
    ptToTest : POINTER TO BYTE;
    iSize    : DINT;
    iGran    : DINT;
    bWrite   : BOOL;
END_VAR

Implementation (incomplete!):

// No standard way of implementation. Fill your own code here
{noflow}
CheckPointer := ptToTest;
{flow}

When the function is called, TwinCAT transfers the following input parameters to it:

If the check result is positive, the input pointer is returned unchanged (ptToTest).

Example:

The following implementation example displays a message in the TwinCAT output window as soon as an invalid pointer is recognized. This implementation recognizes various types of invalid pointers. However, it cannot recognize all invalid pointers.

// Implicitly generated code : DO NOT EDIT
FUNCTION CheckPointer : POINTER TO BYTE 
VAR_INPUT
    ptToTest : POINTER TO BYTE;
    iSize    : DINT;
    iGran    : DINT;
    bWrite   : BOOL;
END_VAR
IF ptToTest=0 THEN
    ADSLOGSTR(ADSLOG_MSGTYPE_ERROR OR ADSLOG_MSGTYPE_STRING,'CheckPointer failed due to invalid destination address.','');
ELSIF iSize<=0 THEN
    ADSLOGSTR(ADSLOG_MSGTYPE_ERROR OR ADSLOG_MSGTYPE_STRING,'CheckPointer failed due to invalid size.','');
ELSIF iGran<=0 THEN
    ADSLOGSTR(ADSLOG_MSGTYPE_ERROR OR ADSLOG_MSGTYPE_STRING,'CheckPointer failed due to invalid granularity.','');
// -> Please note that the following memory area check is time consuming:
//ELSIF F_CheckMemoryArea(pData:=ptToTest,nSize:=DINT_TO_UDINT(iSize)) = E_TcMemoryArea.Unknown THEN
//    ADSLOGSTR(ADSLOG_MSGTYPE_ERROR OR ADSLOG_MSGTYPE_STRING,'CheckPointer failed due to unknown memory area.','');
END_IF
CheckPointer := ptToTest;

CAUTION

Consequence of an invalid pointer

The consequence of an invalid pointer is usually that the runtime is stopped as soon as any access takes place via this pointer. The CheckPointer function cannot usually prevent this. The purpose of this monitoring function is actually to make efficient cause diagnosis possible.