SA0167: Report temporary FunctionBlock instances

Function

Determines function block instances that are declared as temporary variables. This applies to instances that are declared in a method, in a function or as VAR_TEMP, and which are reinitialized in each processing cycle or each function block call.

Reason

  • Function blocks have a state that is usually retained over several PLC cycles. An instance on the stack exists only for the duration of the function call. It is therefore only rarely useful to create an instance as a temporary variable.
  • Secondly, function block instances are frequently large and require a great deal of space on the stack (which is usually limited on controllers).
  • Thirdly, the initialization and often also the scheduling of the function block can take up quite a lot of time.

Importance

Medium

Samples:

Method FB_Sample.SampleMethod:

METHOD SampleMethod : INT
VAR_INPUT
END_VAR
VAR
    fbTrigger : R_TRIG;          // => SA0167
END_VAR

Function F_Sample:

FUNCTION F_Sample : INT
VAR_INPUT
END_VAR
VAR
    fbSample  : FB_Sample;       // => SA0167
END_VAR

MAIN program:

PROGRAM MAIN
VAR_TEMP
    fbSample  : FB_Sample;       // => SA0167
    nReturn   : INT;
END_VAR
nReturn := F_Sample();