Finalization

Overview

Duktape supports object finalization as a custom feature. A finalizer is called when an object is about to be freed, so that application code can e.g. free native resources associated with the object. The finalizer can be either an ECMAScript function or a Duktape/C function. However, ECMAScript finalizers may interact badly with script timeouts, see below.

See How to use finalization for examples.

Getting and setting the current finalizer

An object which has an internal _Finalizer property in its prototype chain (or in the object itself) is subject to finalization before being freed. The internal property should not be accessed directly, but can be read/written using the following:

Finalizer function arguments and return value

The finalizer function is called with two arguments:

The return value of a finalizer is ignored. Any errors thrown by the finalizer are also silently ignored.

Finalizer execution guarantees

The main finalizer guarantees are:

Together these guarantee that a finalizer gets executed at some point before a heap is destroyed, which allows native resources (such as sockets and files) to be freed reliably. There are a few exceptions to this guarantee, see below for more discussion:

When the Duktape heap is being destroyed there are a few limitations for finalizer behavior:

Other current limitations