Specifics of FB_exit Behavior in TwinCAT
Understanding when and how FB_exit is called automatically in TwinCAT, including key compiler limitations and behaviors.
FB_exit should be called automatically every time an object is removed from memory. It all sounds good in theory, but in practice, different compiler versions have had nuances with this.
-
There were cases where
FB_exitshould have been called but wasn’t. So those who still work with older versions should be especially careful. In the latest compiler available at the moment (TwinCAT 3.1.4026.19) — I’m aware of only one problem: if an object was created as a local variable inside a method, that method was called within a__TRY/__CATCHblock, and an exception occurred, thenFB_exitwill not be called. -
FB_exitis called only if the deletion is performed through a pointer to the function block type. Otherwise, the memory will simply be cleared, butFB_exitwill not be called. Below is a demonstration of this behavior, which applies to all versions.

#TwinCAT #CoDeSys #OOP #StructuredText #IndustrialAutomation #FbExit