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 has the methods FB_init and MyInit. FB_init calls MyInit for initialization.
- Function block FB_Sub is derived from FB_Base.
- FB_Sub.MyInit overwrites or extends FB_Base.MyInit.
- MAIN instantiates FB_Sub. During this process it uses the instance variable nSub before it was initialized, due to the call sequence during the initialization.
Function block FB_Base:
FUNCTION_BLOCK FB_Base
VAR
nBase : DINT;
END_VARMethod FB_Base.FB_init:
METHOD FB_init : BOOL
VAR_INPUT
bInitRetains : BOOL;
bInCopyCode : BOOL;
END_VAR
VAR
nLocal : DINT;
END_VARnLocal := MyInit(); // => SA0106Method FB_Base.MyInit:
METHOD MyInit : DINTnBase := 123; // access to member of FB_Base
MyInit := nBase;Function block FB_Sub:
FUNCTION_BLOCK FB_Sub EXTENDS FB_Base
VAR
nSub : DINT;
END_VARMethod FB_Sub.MyInit:
METHOD MyInit : DINTnSub := 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:
- nBase is 123
The instance MAIN.fbSub has the following variable values after the initialization:
- nBase is 123
- nSub is 0
The variable MAIN.fbSub.nSub is 0 after the initialization, because the following call sequence is used during the initialization of fbSub:
- Initialization of the basic function block:
- implicit initialization
- explicit initialization: FB_Base.FB_init
- FB_Base.FB_init calls FB_Sub.MyInit → SA0106
- FB_Sub.MyInit calls FB_Base.MyInit (via SUPER pointer)
- Initialization of the derived function block:
- implicit initialization