AvaloniaUI/Avalonia · error · ArgumentNullException

Value cannot be null. (Parameter 'compositeDisposable')

Error message

Value cannot be null. (Parameter 'compositeDisposable')

What it means

Thrown by the DisposeWith(this T item, CompositeDisposable compositeDisposable) extension when compositeDisposable is null. DisposeWith is a fluent convenience that registers an IDisposable into a CompositeDisposable so the whole group can be torn down together. A null composite would silently drop the registration and leak the item, so it is rejected up front.

Source

Thrown at src/Avalonia.Base/Reactive/DisposableMixin.cs:31

    /// </summary>
    /// <typeparam name="T">
    /// The type of the disposable.
    /// </typeparam>
    /// <param name="item">
    /// The disposable we are going to want to be disposed by the CompositeDisposable.
    /// </param>
    /// <param name="compositeDisposable">
    /// The <see cref="CompositeDisposable"/> to which <paramref name="item"/> will be added.
    /// </param>
    /// <returns>
    /// The disposable.
    /// </returns>
    public static T DisposeWith<T>(this T item, CompositeDisposable compositeDisposable)
        where T : IDisposable
    {
        if (compositeDisposable is null)
        {
            throw new ArgumentNullException(nameof(compositeDisposable));
        }

        compositeDisposable.Add(item);
        return item;
    }
}

View on GitHub (pinned to 11c5427268)

Solutions

  1. Initialize the CompositeDisposable field/property before any DisposeWith call (assign it inline or in the constructor before subscriptions).
  2. Null-check the composite at the call site and skip DisposeWith if null, or create one on demand.
  3. Stop nulling out the composite on dispose — dispose it but keep the reference, or stop subscribing after disposal.

Example fix

// before
private CompositeDisposable _disposables; // never assigned
sub.Subscribe(handler).DisposeWith(_disposables);
// after
private readonly CompositeDisposable _disposables = new();
sub.Subscribe(handler).DisposeWith(_disposables);
Defensive patterns

Strategy: validation

Validate before calling

if (compositeDisposable is null) throw new ArgumentNullException(nameof(compositeDisposable));
item.DisposeWith(compositeDisposable);

Type guard

static bool IsUsable(CompositeDisposable? cd) => cd is not null;

Prevention

When it happens

Trigger: Calling someDisposable.DisposeWith(null) or DisposeWith(_disposables) where _disposables has not been initialized or was already disposed and nulled out.

Common situations: Field initialization order: DisposeWith is called in a constructor before the CompositeDisposable field is assigned; the composite was disposed and the field set to null in cleanup code; a null composite is passed from a property whose backing field is lazy-initialized but not yet accessed.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/9517bc1afe93b7e5. Report an issue: GitHub.