SA0106: Virtual method calls in FB_init

Function

Determines method calls in the method FB_init of a basic function block, which are overwritten by a function block derived from the basic FB.

Reason

In such cases it may happen that the variables in overwritten methods are not initialized in the base FB.

Importance

High

Sample:

Function block FB_Base:

FUNCTION_BLOCK FB_Base
VAR
    nBase        : DINT;
END_VAR

Method FB_Base.FB_init:

METHOD FB_init : BOOL
VAR_INPUT
    bInitRetains : BOOL;
    bInCopyCode  : BOOL;
END_VAR
VAR
    nLocal       : DINT;
END_VAR
nLocal := MyInit();              // => SA0106

Method FB_Base.MyInit:

METHOD MyInit : DINT
nBase  := 123;                   // access to member of FB_Base
MyInit := nBase;

Function block FB_Sub:

FUNCTION_BLOCK FB_Sub EXTENDS FB_Base
VAR
    nSub         : DINT;
END_VAR

Method FB_Sub.MyInit:

METHOD MyInit : DINT
nSub   := 456;                   // access to member of FB_Sub
SUPER^.MyInit();                 // call of base implementation
MyInit := nSub;

MAIN program:

PROGRAM MAIN
VAR
    fbBase       : FB_Base;
    fbSub        : FB_Sub;
END_VAR

 

The instance MAIN.fbBase has the following variable values after the initialization:

The instance MAIN.fbSub has the following variable values after the initialization:

The variable MAIN.fbSub.nSub is 0 after the initialization, because the following call sequence is used during the initialization of fbSub: