An init function and/or a deinit function can be declared by
foreign_resource/2. If this is the case, these functions should
have the prototype:
The init function is called by
after the resource has been loaded and the interfaced predicates
have been installed. If the init function fails (using
SP_fail()) or raises an exception (using
SP_raise_exception()), the failure or exception is propagated by
load_foreign_resource/1 and the foreign resource is
unloaded (without calling any deinit function). However, using
SP_fail() is not recommended, and operations that may require
SP_raise_exception() are probably better done in an init
function that is called explicitly after the foreign resource has
The deinit function is called by
before the interfaced predicates have been uninstalled and the
resource has been unloaded. If the deinit function fails or
raises an exception, the failure or exception is propagated by
unload_foreign_resource/1, but the foreign resource is
be called in a deinit function.
Complex deinitialization should be done in an explicitly
called deinit function instead.
The init and deinit functions may use the C-interface to call Prolog etc.
Foreign resources are unloaded
when the saved-state is restored; see Saving.
Foreign resources are also unloaded when
exiting Prolog execution. The parameter
when reflects the context
(un)load_foreign_resource/1 and is set as follows for
For deinit functions:
unload_foreign_resource/1or a call to
load_foreign_resource/1with the name of an already loaded resource.