Very cool! I wasn't aware of the cleanup attribute, can't wait to try it out:
The cleanup attribute runs a function when the variable goes out of scope. This attribute can only be applied to auto function scope variables; it may not be applied to parameters or variables with static storage duration. The function must take one parameter, a pointer to a type compatible with the variable. The return value of the function (if any) is ignored.
https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attribute...