This article covers features introduced in SpiderMonkey 17

Add/remove callback function for finalization.


JS_AddFinalizeCallback(JSRuntime *rt, JSFinalizeCallback cb, void *data); // Added in SpiderMonkey 38 (JSAPI 32)

JS_RemoveFinalizeCallback(JSRuntime *rt, JSFinalizeCallback cb); // Added in SpiderMonkey 38 (JSAPI 32)

JS_SetFinalizeCallback(JSRuntime *rt, JSFinalizeCallback cb); // Obsolete since JSAPI 32
Name Type Description
rt JSRuntime * The JSRuntime for which to set the finalization callback.
cb JSFinalizeCallback Pointer to the new callback function to use.
data void * Passed to data parameter for JSFinalizeCallback.

Callback syntax

typedef enum JSFinalizeStatus {
} JSFinalizeStatus;

typedef void
(* JSFinalizeCallback)(JSFreeOp *fop, JSFinalizeStatus status, bool isCompartment, void *data);
Name Type Description
fop JSFreeOp * A pointer to an instance of JSFreeOp.
status JSFinalizeStatus One of the JSFinalizeStatus constants, described below, indicating the stage of finalize.
isCompartment bool false if all compartments are being collected, true if a compartment is being collected.
data void * data parameter specified in JS_AddFinalizeCallback. Added in SpiderMonkey 38
Name Description
JSFINALIZE_GROUP_START Called when preparing to sweep a group of compartments, before anything has been swept. The collector will not yield to the mutator before calling the callback with JSFINALIZE_GROUP_END status.
JSFINALIZE_GROUP_END Called when preparing to sweep a group of compartments. Weak references to unmarked things have been removed and things that are not swept incrementally have been finalized at this point. The collector may yield to the mutator after this point.
JSFINALIZE_COLLECTION_END Called at the end of collection when everything has been swept.


JS_AddFinalizeCallback add a callback function which the garbage collector calls at several points during garbage collection. rt is the runtime in which you specify the callback. cb is a pointer to the callback function to add.

JS_RemoveFinalizeCallback removes the previously added callback function.

Obsolete since JSAPI 32

JS_SetFinalizeCallback sets a new callback function. It was replaced with JS_AddFinalizeCallback, to allow adding multiple callback functions.

See Also