chickadee » chicken » gc » set-finalizer!

set-finalizer! X PROCprocedure

Registers a procedure of one argument PROC, that will be called as soon as the non-immediate data object X is about to be garbage-collected (with that object as its argument). This procedure returns X.

Finalizers installed using set-finalizer! are invoked asynchronously, in the thread that happens to be currently running. Finalizers for data that has become garbage are called on normal program exit. Finalizers are not run on abnormal program exit. A normal program exit does not run finalizers that are still reachable from global data.

Multiple finalizers can be registered for the same object. The order in which the finalizers run is undefined. Execution of finalizers may be nested.

NOTE 1: The finalizer will not be called while interrupts are disabled.

NOTE 2: When a finalizable object has any weak references (i.e., weak locatives or weak pairs) to objects that are only reachable through it or other finalizable objects, those references will be broken like when the objects had already been collected. This is done in order to avoid user code from accessing objects that are possibly in an invalid state.