Questions about foreign frames

Hi!

I have been using SWI-Prolog for the backend of a Win32 GUI application in C with great success, but I am still a little unsure whether I have understood how data should be properly discarded. More precisely, my question is when to use PL_open/close/discard_foreign_frame.

As I understand it, PL_open_query creates a query + other data. The query is discarded by PL_cut/close_query, but the data is destroyed only by PL_close_query and not by PL_cut_query.

Am I correct in assuming that PL_open_foreign_frame and PL_close_foreign_frame are not needed around PL_open+close_query, but should be used around PL_open+cut_query?

Secondly, I am unsure as to which of PL_close_foreign_frame and PL_discard_foreign_frame is preferable, generally. Up until now, I’ve added PL_open+discard_foreign_frame around any interaction with Prolog from C.

I’d be very grateful for any clarifications!