rust_cc

Module cleaners

Source
Available on crate feature cleaners only.
Expand description

Execute cleaning actions on object destruction.

Cleaners can be used to register cleaning actions, which are executed when the Cleaner in which they’re registered is dropped.

Adding a Cleaner field to a struct makes it possible to execute cleaning actions on object destruction.

§Cleaning actions

A cleaning action is provided as a closure to the register method, which returns a Cleanable that can be used to manually run the action.

Every cleaning action is executed at maximum once. Thus, any manually-run action will not be executed when their Cleaner is dropped. The same also applies to cleaning actions run manually after the Cleaner in which they were registered is dropped, as they have already been executed.

§Avoiding memory leaks

Usually, Cleaners are stored inside a cycle-collected object. Make sure to never capture a reference to the container object inside the cleaning action closure, otherwise the object will be leaked and the cleaning action will never be executed.

§Cleaners vs finalization

Cleaners provide a faster alternative to finalization. As such, when possible it’s suggested to prefer cleaners and disable finalization.

Structs§