{"record":{"id":"0ee387c1ee152201","repo":"dotnet/wpf","slug":"sr-freezable-notacontext","errorCode":null,"errorMessage":"SR.Freezable_NotAContext","messagePattern":"SR\\.Freezable_NotAContext","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Freezable.cs","lineNumber":1088,"sourceCode":"                    {\n                        ++deadRefs;\n                    }\n                }\n\n                if (index != -1)\n                {\n                    Debug.Assert(!failed);\n\n                    list.RemoveAt(index);\n                }\n\n                PruneContexts(list, deadRefs);\n            }\n\n            // Make sure we actually removed something - if not throw an exception\n            if (failed)\n            {\n                throw new ArgumentException(SR.Freezable_NotAContext, nameof(context));\n            }\n        }\n\n        /// <summary>\n        /// Removes the single piece of contextual information that we have and updates all flags\n        /// accordingly.\n        /// </summary>\n        private void RemoveSingletonContext()\n        {\n            Debug.Assert(Freezable_UsingSingletonContext);\n            Debug.Assert(SingletonContext != null);\n\n            if (HasHandlers)\n            {\n                _contextStorage = ((HandlerContextStorage)_contextStorage)._handlerStorage;\n            }\n            else\n            {","sourceCodeStart":1070,"sourceCodeEnd":1106,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Freezable.cs#L1070-L1106","documentation":"Freezable.RemoveContextInformation removes a recorded inheritance/context reference (a DO/DP pair pointing at this Freezable). If the supplied context is not actually registered on this Freezable, nothing was removed, and the code throws ArgumentException(SR.Freezable_NotAContext) with paramName 'context'. It guards the internal context bookkeeping from corruption.","triggerScenarios":"Calling RemoveContextInformation (via RemoveInheritanceContext or internal RemoveContext APIs) with a DependencyObject that is not a registered owner/inheritance context of this Freezable, or removing the same context twice.","commonSituations":"Custom Freezable subclasses reimplementing inheritance-context plumbing incorrectly; double-detaching a shared Brush/Animation from a property; re-entrancy during property invalidation removing contexts out of order.","solutions":["Only call context-removal APIs for contexts currently registered (verify via GetBoolValue/inheritance context lists)","Avoid double-removal: check whether the context is still attached before removing","Let WPF's property system manage inheritance contexts instead of manipulating them manually","Reproduce under a debugger to find the unbalanced add/remove pair"],"exampleFix":"// before\nfreezable.RemoveInheritanceContext(otherOwner, someDp); // not a context\n// after\nif (ReferenceEquals(freezable.InheritanceContext, otherOwner))\n    freezable.RemoveInheritanceContext(otherOwner, someDp);","handlingStrategy":"try-catch","validationCode":"if (ReferenceEquals(freezable.InheritanceContext, context)) freezable.RemoveInheritanceContext(context, dp);","typeGuard":null,"tryCatchPattern":"try { freezable.RemoveInheritanceContext(owner, dp); }\ncatch (ArgumentException ex) { log.Warn(\"Context was not registered on this Freezable\", ex); }","preventionTips":["Never remove the same inheritance context twice","Let the WPF property system manage inheritance contexts on stock Freezables","Add remove-pairing asserts in custom Freezable subclasses"],"tags":["wpf","freezable","inheritance-context","argument"],"backgroundTag":"internal-invariant-violation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}