On this page:
unsafe-add-collect-callbacks
unsafe-remove-collect-callbacks

5.14 Garbage Collection Callbacks

The ffi/unsafe/collect-callback library provides functions to register constrained callbacks that are run just before and after a garbage collection.

Added in version 7.0.0.9 of package base.

procedure

(unsafe-add-collect-callbacks pre post)  any/c

  pre : (vectorof vector?)
  post : (vectorof vector?)
Registers descriptions of foreign functions to be called just before and just after a garbage collection. The foreign functions must not allocate garbage-collected memory, and they are called in a way that does not allocate, which is why pre_desc and post_desc are function descriptions instead of thunks.

A description is a vector of vectors, where each of the inner vectors describes a single call, and the calls are performed in sequence. Each call vector starts with a symbol that indicates the protocol of the foreign function to be called. The following protocols are supported: The apparently arbitrary and whimsical set of supported protocols is enough to allow DrRacket to show a garbage-collection icon.

After the protocol symbol, the vector should contain a pointer to a foreign function and then an element for each of the function’s arguments. Pointer values are represented as for the _pointer representation defined by ffi/unsafe.

The result is a key for use with unsafe-remove-collect-callbacks. If the key becomes inaccessible, then the callback will be removed automatically (but beware that the pre-callback will have executed and the post-callback will not have executed)

procedure

(unsafe-remove-collect-callbacks key)  void?

  key : any/c
Unregisters pre- and post-collection callbacks that were previously registered by a call to unsafe-add-collect-callbacks that returned v.