Jan,
Thank you so much for providing swipl -d chk_secure
, without it, it would have been almost impossible to debug an error in my foreign code!
Two warnings for all those who are doing non deterministic foreign predicates:
- don’t write code that references the
term_t
arguments outside of theswitch
statement which checks if the call isPL_FIRST_CALL
,PL_REDO
orPL_PRUNED
.
2) don’t use theterm_t
references inside thePL_PRUNED
case.
Longer explanation:
If an exception is thrown (after the foreign predicate returned with one of PL_retry
s), the term_t
arguments are not valid anymore, and the the function is called again with control_t
call type set to PL_PRUNED
(this is needed to cleanup memory).
If you have code that uses the term_t
s it will crash the system, eventually causing the garbage collector will bail out with a segfault.