12.4.24 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_winitialise()
            • 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_and_flags)
This function may be called instead of PL_halt() to cleanup Prolog without exitting the process. It 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), restores all signal handlers and reclaims all memory unless asked not to. status_and_flags accepts the following flags:
PL_CLEANUP_NO_RECLAIM_MEMORY
Do not reclaim memory. This is the default when called from PL_halt() for the release versions because the OS will do so anyway.
PL_CLEANUP_NO_CANCEL
Do not allow Prolog and foreign halt hooks to cancel the cleanup.

The return value of PL_cleanup() is one of the following:

PL_CLEANUP_CANCELED
A Prolog or foreign halt hook cancelled the cleanup. Note that some of the halt hooks may have been executed.
PL_CLEANUP_SUCCESS
Cleanup completed successfully. Unless PL_CLEANUP_NO_RECLAIM_MEMORY was specified this implies most of the memory was reclaimed and Prolog may be reinitialized in the same process using PL_initialise().
PL_CLEANUP_FAILED
Cleanup failed. This happens if the user requested to reclaim all memory but this failed because the system was not able to join all Prolog threads and could therefore not reclaim the memory.
PL_CLEANUP_RECURSIVE
PL_cleanup() was called from a hook called by the cleanup process.

PL_cleanup() allows deleting and restarting the Prolog system in the same process. In versions older than 8.5.9 this did not work. As of version 8.5.9, it works for the basic Prolog engine. Many of the plugins that contain foreign code do not implement a suitable uninstall handler and will leak memory and possibly other resources. Note that shutting Prolog down and renitializing it is slow. For almost all scenarios there are faster alternatives such as reloading modified code, using temporary modules, using threads, etc.