JSFinalizeOp

JSFinalizeOp is the type of JSClass.finalize.

Syntax

typedef void
(* JSFinalizeOp)(JSFreeOp *fop, JSObject *obj);
Name Type Description
cx JSContext * The JS context in which garbage collection is taking place.
obj JSObject * The object being destroyed.

Description

The JSFinalizeOp is analogous to Java finalizers or C++ destructors. The garbage collector calls this callback for each object it collects. The finalizer's job is to clean up any resources allocated by the instance which wouldn't normally be cleaned up by the garbage collector (private data stored in the object by the application, file handles, etc.)

Finalizers must never store a reference to obj.

Warning: This hook is called during garbage collection. Any JSAPI call that would allocate memory from the GC heap will fail if called from a finalizer.

For other ways to interact with garbage collection (e.g. implementing weak references), see JS_SetGCCallback.

JSClass hooks

JSClass offers the following hook:

  • The JSClass.finalize callback is a hook for destructor code. The garbage collector calls the finalize hook of each JSObject it collects.

See Also