l0.DependencyControl.Finalizer¶
Finalizer ¶
Runs a cleanup action when a value is garbage-collected — a last-resort release for OS handles, locks, and the like when a caller forgets to release explicitly.
Class methods¶
create ¶
Creates a finalizer that runs the given cleanup once, when it is collected. Store it on the object whose lifetime should trigger the cleanup. The cleanup must capture the resources it frees, not the owning object — capturing the owner keeps it reachable, so the finalizer never runs.
| param | type | description |
|---|---|---|
| cleanup | fun() |
Runs once when the finalizer is collected; its errors are swallowed. |
Returns:
finalizeruserdata— An opaque value to anchor; it exposes no usable fields.
guard ¶
Creates a finalizer and pins it to the anchor under a private key, so the cleanup runs when the anchor is collected. Pass an instance to release its resources on GC, or a class to tie the cleanup to the module's lifetime. The cleanup must capture the resources it frees, not the anchor.
| param | type | description |
|---|---|---|
| anchor | table |
The instance or class whose collection triggers the cleanup. |
| cleanup | fun() |
Runs once when the anchor is collected; its errors are swallowed. |
Returns:
anchortable— The same anchor, for chaining.