# Thank you! (foreign non-deterministic predicates)

**URL:** <https://swi-prolog.discourse.group/t/thank-you-foreign-non-deterministic-predicates/538>\
**Category:** General\
**Created:** [April 14, 2019, 4:05am UTC](https://swi-prolog.discourse.group/t/thank-you-foreign-non-deterministic-predicates/538 "2019-04-14T04:05:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![swi](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@swi](https://swi-prolog.discourse.group/u/swi)\
**Post date:** [April 14, 2019, 4:05am UTC](https://swi-prolog.discourse.group/t/thank-you-foreign-non-deterministic-predicates/538/1 "2019-04-14T04:05:51Z")

</div>

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:

1. don’t write code that references the `term_t` arguments _outside_ of the `switch` statement which checks if the call is `PL_FIRST_CALL`, `PL_REDO` or `PL_PRUNED`.  
2) don’t use the `term_t` references inside the `PL_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.
