12.4.22 Embedding SWI-Prolog in other applications
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
      • Foreign Language Interface
        • The Foreign Include File
          • Embedding SWI-Prolog in other applications
            • PL_initialise()
            • PL_is_initialised()
            • PL_set_resource_db_mem()
            • PL_toplevel()
            • PL_cleanup()
            • PL_cleanup_fork()
            • PL_halt()
            • Threading, Signals and embedded Prolog
    • Packages
Availability:C-language interface function
int PL_cleanup(int status)
This function performs the reverse of PL_initialise(). It runs the PL_on_halt() and at_halt/1 handlers, closes all streams (except for the `standard I/O' streams which are flushed only), deallocates all memory if status equals `0' and restores all signal handlers. The status argument is passed to the various termination hooks and indicates the exit-status.

The function returns TRUE if successful and FALSE otherwise. Currently, FALSE is returned when an attempt is made to call PL_cleanup() recursively or if one of the exit handlers cancels the termination using cancel_halt/1. Exit handlers may only cancel termination if status is 0.

In theory, this function allows deleting and restarting the Prolog system in the same process. In practice, SWI-Prolog's cleanup process is far from complete, and trying to revive the system using PL_initialise() will leak memory in the best case. It can also crash the appliction.

In this state, there is little practical use for this function. If you want to use Prolog temporarily, consider running it in a separate process. If you want to be able to reset Prolog, your options are (again) a separate process, modules or threads.