# Ownership and cleanup

Source: https://libtmux.org/en/swift/latest/topics/context-managers/

> Scope-based cleanup for tmux objects, and when your program must kill them explicitly.

A tmux session, window, or pane normally remains until you kill it. Scope-based
cleanup can kill it when your code leaves a block, including after an exception.
See [Workspaces](https://libtmux.org/en/swift/latest/concepts/workspaces/) for a temporary layout example.

## Kill objects your program owns

Session, window, and pane values are non-owning. Call
[`try await server.kill(session)`](<https://libtmux.org/en/swift/latest/reference/server-kill(_-)/>) or the corresponding window or pane overload
when cleanup is required. Perform cleanup on both success and failure paths;
Swift's synchronous `defer` cannot await a tmux command.

<a id="what-this-means-in-practice"></a>

## Testing cleanup

Use explicit cleanup for objects whose handles have no disposal hook. For an
entire disposable test server, prefer your port's test fixture or server guard;
see [Testing with libtmux](https://libtmux.org/en/swift/latest/guides/testing-with-libtmux/).
