# Consequence of not closing foreign frames

**URL:** <https://swi-prolog.discourse.group/t/consequence-of-not-closing-foreign-frames/1438>\
**Category:** General\
**Created:** [October 30, 2019, 11:19am UTC](https://swi-prolog.discourse.group/t/consequence-of-not-closing-foreign-frames/1438 "2019-10-30T11:19:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![maren](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/maren/32/6605_2.png) [@maren](https://swi-prolog.discourse.group/u/maren)\
**Post date:** [October 30, 2019, 11:19am UTC](https://swi-prolog.discourse.group/t/consequence-of-not-closing-foreign-frames/1438/1 "2019-10-30T11:19:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [November 1, 2019, 2:12am UTC](https://swi-prolog.discourse.group/t/consequence-of-not-closing-foreign-frames/1438/2 "2019-11-01T02:12:52Z")

</div>

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 🙂
