{"record":{"id":"65bf4e4112986af6","repo":"AvaloniaUI/Avalonia","slug":"owner","errorCode":null,"errorMessage":"owner","messagePattern":"owner","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Styling/StyleBase.cs","lineNumber":160,"sourceCode":"                    instance.Add(_animations);\n\n                if (canShareInstance)\n                {\n                    instance.MakeShared();\n                    _sharedInstance = instance;\n                }\n            }\n\n            ao.GetValueStore().AddFrame(instance);\n            instance.ApplyAnimations(ao);\n            return instance;\n        }\n\n        internal virtual void SetParent(StyleBase? parent) => Parent = parent;\n\n        void IResourceProvider.AddOwner(IResourceHost owner)\n        {\n            owner = owner ?? throw new ArgumentNullException(nameof(owner));\n\n            if (Owner != null)\n            {\n                throw new InvalidOperationException(\"The Style already has a parent.\");\n            }\n\n            Owner = owner;\n            _resources?.AddOwner(owner);\n        }\n\n        void IResourceProvider.RemoveOwner(IResourceHost owner)\n        {\n            owner = owner ?? throw new ArgumentNullException(nameof(owner));\n\n            if (Owner == owner)\n            {\n                Owner = null;\n                _resources?.RemoveOwner(owner);","sourceCodeStart":142,"sourceCodeEnd":178,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Styling/StyleBase.cs#L142-L178","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\n((IResourceProvider)style).AddOwner(null);\n\n// after\n((IResourceProvider)style).AddOwner(ownerHost);","handlingStrategy":"validation","validationCode":"// Validate owner before registering resources.\nif (ownerHost is null) throw new ArgumentNullException(nameof(ownerHost));\n((IResourceProvider)style).AddOwner(ownerHost);","typeGuard":"static bool IsValidResourceOwner(IResourceHost? host) => host is not null;","tryCatchPattern":null,"preventionTips":["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."],"tags":["avalonia","styling","resources","null-argument","ownership"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}