with-destructors ( quot -- )
Factor documentation > Factor handbook > The language > Deterministic resource disposal > Using destructors
Prev:dispose-each ( seq -- )
Next:&dispose ( disposable -- disposable )


Vocabulary
destructors

Inputs and outputs
quota quotation


Word description
Calls a quotation within a new dynamic scope. This quotation may register destructors using &dispose or |dispose. The former registers a destructor that will always run whether or not the quotation threw an error, and the latter registers a destructor that only runs if the quotation throws an error. Destructors are run in reverse order from the order in which they were registered.

Notes
Destructors generalize with-disposal. The following two lines are equivalent, except that the second line establishes a new dynamic scope:
[ X ] with-disposal [ &dispose X ] with-destructors


Examples
[ 10 malloc &free ] with-destructors


Definition
USING: continuations destructors.private kernel namespaces ;

IN: destructors

: with-destructors ( quot -- )
[
V{ } clone always-destructors set
V{ } clone error-destructors set
[ do-always-destructors ] [ do-error-destructors ]
cleanup
] with-scope ; inline