SA0046: Possible use of not initialized interfaces

Function

Determines the use of interfaces that may not have been initialized before the use.

Reason

An interface reference should be checked for <> 0 before it is used, otherwise an access violation may occur at runtime.

Importance

High

Samples:

Interface I_Sample:

INTERFACE I_Sample
METHOD SampleMethod : BOOL
VAR_INPUT
    nInput  : INT;
END_VAR

Function block FB_Sample:

FUNCTION_BLOCK FB_Sample IMPLEMENTS I_Sample
METHOD SampleMethod : BOOL
VAR_INPUT
    nInput  : INT;
END_VAR

MAIN program:

PROGRAM MAIN
VAR
    fbSample      : FB_Sample;
    iSample       : I_Sample;
    iSampleNotSet : I_Sample;
    nParam        : INT;
    bReturn       : BOOL;
END_VAR
iSample := fbSample; 
bReturn := iSample.SampleMethod(nInput := nParam);         // no error
 
bReturn := iSampleNotSet.SampleMethod(nInput := nParam);   // => SA0046

Overview of the rules on "dereferencing"

Pointer

Dereferencing of pointers in the declaration part

SA0124: Dereference access in initializations

Possible null pointer dereferences in the implementation part

SA0039: Possible null pointer dereferences

References

Use of references in the declaration part

SA0125: References in initializations

Possible use of not initialized reference in the implementation part

SA0145: Possible use of not initialized references

Interfaces

Possible use of not initialized interface in the implementation part

SA0046: Possible use of not initialized interfaces