Mozilla.com

Table of contents
  1. 1. Syntax
  2. 2. Description
Table of contents
  1. 1. Syntax
  2. 2. Description

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

Syntax

typedef void (*JSFinalizeOp)(JSContext *cx, 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 JSClass.finalize callback 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 sign
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.

Page last modified 15:53, 5 Feb 2008 by Jorend

Files (0)