Skip to content

GuidesOwn and dispose

Own and dispose

Who disposes a data source, when a provider does and when you do, and how a source disposes its items.

A data source can hold a timer, an event subscription or disposable items, so someone has to dispose it. The owns flag says who. The rules in full are on data sources and ownership.

A provider made from one source defaults owns to true, so disposing the provider disposes the source. The source then throws ObjectDisposedException when you read its Collection.

var dataSourceFactory = serviceProvider.GetRequiredService<ICollectionDataSourceFactory>();
var providerFactory = serviceProvider.GetRequiredService<ICollectionProviderFactory>();
var observable = new ObservableCollection<string> { "alpha" };
var dataSource = dataSourceFactory.CreateObservable(observable);
// A single source defaults to owns: true, so the provider disposes it.
var provider = providerFactory.Create(items => items.ToList(), dataSource);
await provider.DisposeAsync();

A provider made from several sources defaults owns to false, because such sources usually come from a DI container. Disposing the provider leaves them open and readable, and you dispose them.

var dataSourceFactory = serviceProvider.GetRequiredService<ICollectionDataSourceFactory>();
var providerFactory = serviceProvider.GetRequiredService<ICollectionProviderFactory>();
var firstItems = new ObservableCollection<string> { "alpha" };
var secondItems = new ObservableCollection<string> { "beta" };
using var first = dataSourceFactory.CreateObservable(firstItems);
using var second = dataSourceFactory.CreateObservable(secondItems);
// Several sources default to owns: false, so you dispose them.
var provider = providerFactory.Create(items => items.ToList(), dataSources: [first, second]);
await provider.DisposeAsync();
var stillReadable = first.Collection.Concat(second.Collection).ToList();

A static or observable source also has an owns flag, which defaults to true and covers its items: disposing the source disposes each item when the item type T is a reference type that implements IDisposable. An item typed as object, as an interface without IDisposable or as a struct is left alone. In the sample the provider owns the static source and the source owns its Connection, a small type that records whether it was disposed, so disposing the provider disposes the source, which disposes the item. Pass owns: false when the caller still uses the items.

var dataSourceFactory = serviceProvider.GetRequiredService<ICollectionDataSourceFactory>();
var providerFactory = serviceProvider.GetRequiredService<ICollectionProviderFactory>();
var connection = new Connection();
// The static source owns its items by default, and the provider owns the source.
var dataSource = dataSourceFactory.CreateStatic([connection]);
var provider = providerFactory.Create(items => items.ToList(), dataSource);
await provider.DisposeAsync();
  • Expecting AsDataSource() to dispose the wrapped provider.

    • You write outer = Create(items => items.ToList(), inner.AsDataSource()) and dispose only outer.
    • What happens: the adapter disposes nothing, so inner and its own source stay open.
    • Write instead: dispose inner yourself, and pass owns: false to the outer provider.
  • A static source disposing items the caller still uses.

    • You write CreateStatic(connections) with the default owns, then keep using the connections after the source is disposed.
    • What happens: disposing the source disposes each item when the item type is a reference type that implements IDisposable.
    • Write instead: pass owns: false when the caller keeps the items.
  • Reading after disposal.

    • You write dataSource.Collection after the source, or the provider that owns it, is disposed.
    • What happens: it throws ObjectDisposedException, except on the static source, which keeps serving its copy.
    • Write instead: read only while the owner is alive, and dispose in the owner’s own shutdown.