Vixen
02b45cc4
csharp
public sealed class DisposeBag

Collects the things a subsystem owns so teardown is one call instead of a hand-maintained sequence of Disposes that drifts every time a field is added.

No guide page documents this yet — the page shows what the code says about itself.

Remarks

Disposal runs in reverse order of registration, because construction order is dependency order: whatever was built last is the thing that may still be using what was built first.

Every entry is disposed even if one throws; the failures are collected and rethrown together as an AggregateException. A bag that gives up halfway leaks GPU memory, and a leak on a shutdown path is the hardest kind to notice.

Fields and properties (2)

  • public int Count

    How many entries are waiting to be disposed.

  • public bool IsDisposed

    Whether the bag has already been disposed.

Methods (5)

  • public T Add<T>(T disposable) where T : IDisposable

    Takes ownership of and hands it straight back, so it can be wrapped around a construction expression: var device = bag.Add(new Device(…));

  • public T AddAsync<T>(T disposable) where T : IAsyncDisposable

    Takes ownership of an asynchronously disposable resource.

  • public void Add(Action onDispose)

    Registers a callback to run at teardown, in reverse registration order.

  • public void Dispose()

    Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

  • public ValueTask DisposeAsync()

    Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.

Used by (2)

  • DisposeBagTestsVixen.Core.Tests
  • VixenApplicationVixen.App.Hosting