Sub topic 8
Held Values: Passing Data to the Callback
This chapter assumes you know how to create a FinalizationRegistry and call register(), and that you understand reachability and the difference between strong and weak references from the earlier chapters.
Why the Callback Needs a Held Value
When a cleanup callback runs, the target is already gone. You cannot pass the target to the callback, because a collected object no longer exists. The registry passes a held value instead. The held value is the second argument to register(), and it tells the callback what to clean up.
const registry = new FinalizationRegistry((heldValue) => {
console.log(`Cleaned up: ${heldValue}`);
});
let resource = { id: 42 };
registry.register(resource, 'resource-42');
resource = null; // at some later point the callback may run with 'resource-42'The callback receives 'resource-42', never resource itself. The usual design is to make the held value describe the object's external state, such as a cache key, a handle ID, or a native pointer wrapper, so the callback can work without the target.
The Rules for Held Values
| Rule | Consequence |
|---|---|
| Any value is allowed: primitives, objects, `undefined` | You can pass a string key, a number, or an inner handle object |
| The registry holds the value **strongly** | The held value stays alive until the callback runs or the registration is removed |
| The held value cannot be the target | `register(obj, obj)` throws a `TypeError` |
| Each `register()` call is its own registration | Registering the same target twice can run the callback twice |
The rule against using the target as its own held value exists because of the strong hold. If the registry kept the target alive strongly, the target could never become unreachable, and the callback could never run. The engine detects this direct case for you:
const registry = new FinalizationRegistry(() => {});
const obj = {};
try {
registry.register(obj, obj);
} catch (err) {
console.log(err instanceof TypeError); // true: held value cannot be the target
}The Indirect Trap
The engine catches only the identical case. It does not detect a held value that points back to the target, and that mistake fails silently:
const registry = new FinalizationRegistry((info) => {
console.log('never logged for this object');
});
let session = { user: 'ada' };
registry.register(session, { owner: session }); // held value references target
session = null; // still reachable through the registry, so it is never collectedNo error is thrown. The registry holds { owner: session } strongly, that object holds session strongly, and so the target stays reachable forever. A closure that captures the target and is used as the held value causes the same leak.
Choosing a Good Held Value
- A primitive key (string or number) works for map cleanup. The weak-value cache chapter uses this approach.
- An inner resource object works for releasing external state. Pass the file handle, not the wrapper that owns it.
undefinedis fine when the callback only needs to know that something was collected, for example to decrement a counter.
Exercise
A Wrapper class calls registry.register(this, { handle: this.handle, wrapper: this }). No TypeError is thrown. What goes wrong, and how do you fix it?
Show answer
The held value references the wrapper through the wrapper property. The registry holds the held value strongly, so the wrapper stays reachable, is never collected, and the callback never runs. The engine throws only when the held value is the target itself, so this indirect reference goes undetected. Fix it by passing only what cleanup needs: registry.register(this, this.handle).
Exercise
Why can't the registry pass the target to the callback in place of a held value?
Show answer
The callback runs only after the target has been collected, so the target no longer exists. If the registry kept the target around to pass it later, that strong reference would stop the target from ever being collected.