AvaloniaUI/Avalonia · error · ArgumentNullException
owner
Error message
owner
What it means
IResourceProvider.AddOwner registers an IResourceHost (a control, Styles collection, or application) as the owner of this Style's resource dictionary. The method throws ArgumentNullException when owner is null because the resource-ownership lifecycle requires a concrete host to notify on resource changes.
Source
Thrown at src/Avalonia.Base/Styling/StyleBase.cs:160
instance.Add(_animations);
if (canShareInstance)
{
instance.MakeShared();
_sharedInstance = instance;
}
}
ao.GetValueStore().AddFrame(instance);
instance.ApplyAnimations(ao);
return instance;
}
internal virtual void SetParent(StyleBase? parent) => Parent = parent;
void IResourceProvider.AddOwner(IResourceHost owner)
{
owner = owner ?? throw new ArgumentNullException(nameof(owner));
if (Owner != null)
{
throw new InvalidOperationException("The Style already has a parent.");
}
Owner = owner;
_resources?.AddOwner(owner);
}
void IResourceProvider.RemoveOwner(IResourceHost owner)
{
owner = owner ?? throw new ArgumentNullException(nameof(owner));
if (Owner == owner)
{
Owner = null;
_resources?.RemoveOwner(owner);View on GitHub (pinned to 11c5427268)
Solutions
- Pass a non-null IResourceHost instance to AddOwner.
- Prefer letting the framework manage ownership via Styles.Add / Styles.Remove rather than calling AddOwner manually.
- Guard the call site: only invoke AddOwner when you hold a live IResourceHost reference.
Example fix
// before ((IResourceProvider)style).AddOwner(null); // after ((IResourceProvider)style).AddOwner(ownerHost);
Defensive patterns
Strategy: validation
Validate before calling
// Validate owner before registering resources. if (ownerHost is null) throw new ArgumentNullException(nameof(ownerHost)); ((IResourceProvider)style).AddOwner(ownerHost);
Type guard
static bool IsValidResourceOwner(IResourceHost? host) => host is not null;
Prevention
- Let the framework call AddOwner by adding styles to a Styles collection instead of invoking IResourceProvider members directly.
- In custom IResourceHost implementations, never forward null to AddOwner during reparenting.
- Keep owner references in non-nullable fields so they cannot be nulled before registration.
When it happens
Trigger: Calling ((IResourceProvider)style).AddOwner(null) directly, or a custom IResourceHost whose parenting logic forwards null. In normal Avalonia use the framework calls AddOwner automatically when a style is added to a Styles collection, so this only fires under manual or custom resource wiring.
Common situations: Custom Styles subclass overriding resource plumbing; third-party theming libraries that replicate the IResourceProvider contract; teardown code that calls AddOwner with a field that was already nulled.
Related errors
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/65bf4e4112986af6.
Report an issue: GitHub.