Skip to main content
Version: 0.10.0

Function: createResourceScope()

createResourceScope(): ResourceScope

Defined in: core/lifetime.ts:68

Creates a ResourceScope for semi-automatic lifetime management of native resources.

Wrap the allocating body in try/catch, track each resource as it is created, and return scope.dispose as the resulting object's dispose. A failure part-way through then releases everything allocated so far, and a successful build hands the caller the same teardown path.

Returns

ResourceScope

A scope that tracks resources and releases them on dispose.

Example

const scope = createResourceScope();
const dispose = scope.dispose;
try {
const model = scope.track(await wrapAsync(loadModel, runtime)(modelPath));
const { dims } = validateSpec(model.schema, { ... }); // may throw
const tensors = [scope.track(tensor('float32', shape))];
return { run, dispose };
} catch (error) {
dispose();
throw error;
}

We are Software Mansion.