--- myst: html_meta: description: "Configure Melder's disposal methods, understand cleanup order and failure handling, and follow examples of resource and memory ownership." --- # Cleanup and memory ownership **Beginner.** Cleanup without ceremony. **Excerpt.** Define the connection and worker classes with their cleanup methods first. Registration declares how to dispose them; `meld()` creates the instances whose matched methods run when the conduit ends. Declare your teardown **vocabulary** — the method names that mean "clean yourself up" in this system — and Melder calls them on the way down. No base class, no protocol, no context-manager ceremony: ```python book = md.Spellbook() book.bind( spell=PooledConnection, existence="unique", disposal_method_names=["close", "shutdown"], # this binding's ordered methods ) book.bind( spell=BackgroundWorker, existence="unique", disposal_method_names=["shutdown"], ) conduit = book.conjure() connection = conduit.meld("PooledConnection") worker = conduit.meld("BackgroundWorker") conduit.cleanup() # calls each creation's matched methods in their declared order ``` Each bind's list belongs to **that spell**, not every later bind. Melder resolves matching class methods once at bind time and preserves their order: - If the connection implements both names, `close()` runs before `shutdown()`. - The worker uses its own `shutdown()` declaration. - Missing names are omitted; duplicate names execute once. For book-wide methods, use `SpellbookConfiguration`. Its matching methods form one ordered block: last by default, or first with `with_enforce_priority_disposal_methods(True)`. Shared names belong to the book's block in either mode; spell-only names retain their own order. See the [configuration guide](../intermediate/configuration.md) for both arrangements. Within each store, objects are disposed newest first, so a dependency outlives the objects built on it. A failing method does not stop the others: every declared method of every object runs, each failure is chained from what the method raised, and they are raised together as one `ExceptionGroup` once the scope has ended. A `managed_spell_quantity` return normally resets the object for reuse. Its disposal methods run when the pool destroys it — an `elastic` trim, a reset that raises, or the root's teardown. ## Runnable examples - [Disposal contract](../examples/beginner/08-disposal-contract.md) — Beginner 08 - [Explicit cleanup](../examples/beginner/10-explicit-cleanup.md) — Beginner 10 - [Disposal multiple methods](../examples/beginner/35-disposal-multiple-methods.md) — Beginner 35 - [You own the memory now](../examples/beginner/41-you-own-the-memory-now.md) — Beginner 41 [All beginner examples](../examples/beginner/index.md) · [Level contents](index.md) · [Full contents](../contents.md) [Canonical page source](https://github.com/Synaptic724/melder/blob/4b364a338debcb1c010d0f6e9dcbbabb62945ba0/README.md)