TwinCAT Engineering Notes
← All posts

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.

  1. There were cases where FB_exit should 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 / __CATCH block, and an exception occurred, then FB_exit will not be called.

  2. FB_exit is called only if the deletion is performed through a pointer to the function block type. Otherwise, the memory will simply be cleared, but FB_exit will not be called. Below is a demonstration of this behavior, which applies to all versions.

FB_exit Code Demonstration

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