Consequence of not closing foreign frames

Using PL_open_foreign_frame we can open new frames from inside a predicate written in c. There’s also a PL_close_foreign_frame(fid), which I assumed has to be called to match any corresponding frame opens.

However, I just noticed that I am actually not closing my foreign frames properly in some places. As far as I can tell though, nothing has blown up.

Is it actually necessary to close foreign frames? Or can I rely on this cleanup happening anyway when reaching the end of a foreign predicate?

Good question. If a close is forgotten and later followed be a close of an enclosing frame, both are discarded. As a call from Prolog to C creates such a frame and closes it, an opened frame in a foreign predicate doesn’t need to be closed.

If overall control is in C, not closing may result in stack overflows and (not sure) stack corruption.

I’d recommend closing anyway :slight_smile: