I have many methods the return value of which is a REFERENCE TO in my libraries. When I open the library, I see a C0222 error in the message window for each of these. If I compile the library, the message goes away. The code compiles and works under both CODESYS and TwinCAT.
Is this just a CODESYS glitch, or is there a reason for that message?
I can confirm that this happens. Make a PRG or FB with a method that returns a reference to something, the precompiler throws this error:
C0222: Outputs can't be of type REFERENCE TOHowever the code compiles fine, and there are no messages in the normal 'Build' section.(I think that's why the message 'disappears' in your case. The Messages box switches from Precompile to Build when you compile the library.)
I'd suggest listening to the precompiler and avoid returning a reference.
For example:
Call like this:
This code works fine. When you change result you actually change the i variable inside the method. This feels very hacky!
And when you change i from a VAR_INST to a VAR, the code compiles ok, but the reference now points to invalid memory because i goes out of scope when you leave the method.
Edit:
You can use a pragma to disable the error message. Add the following line in every method where you return a reference: