Set REFERENCE to 0 (null) when no longer used

jtebokkel
2020-05-25
2020-05-30
  • jtebokkel

    jtebokkel - 2020-05-25

    Is there a way to work around the error that happens when trying to set a reference to 0 after it isn't needed or is invalid? Trying to do this in codesys >= V3.5.15 results in an error. See example below. Now I could great a GVL with a bunch of unset references, but I'd end up with a large amount of null types.

    METHOD FB_init : BOOL
    VAR_INPUT
        bInitRetains : BOOL; // if TRUE, the retain variables are initialized (warm start / cold start)
        bInCopyCode : BOOL;  // if TRUE, the instance afterwards gets moved into the copy code (online change)
        refLamp : REFERENCE TO FB_Lamp;
        byNewLevel : BYTE;
    END_VAR
    
    IF (__ISVALIDREF(refLamp)) THEN
        THIS^.refLamp REF= refLamp;
    ELSE
        THIS^.refLamp REF= 0;
    END_IF
    THIS^.byNewLevel := byNewLevel;
    
     
  • jtebokkel

    jtebokkel - 2020-05-30

    ok, so I was playing around with some tests today and the code I was looking at I didn't actually try and build/run because I was missing libraries for it.

    Turns out you can build and run

    THIS^.refLamp REF= 0;
    

    it is just the precomplier that complains about it. Is this a bug then in the precompiler?

     

Log in to post a comment.