dotnet/wpf · error · ArgumentException

SR.Freezable_NotAContext

Error message

SR.Freezable_NotAContext

What it means

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.

Solutions

  1. Only call context-removal APIs for contexts currently registered (verify via GetBoolValue/inheritance context lists)
  2. Avoid double-removal: check whether the context is still attached before removing
  3. Let WPF's property system manage inheritance contexts instead of manipulating them manually
  4. Reproduce under a debugger to find the unbalanced add/remove pair

Example fix

// before
freezable.RemoveInheritanceContext(otherOwner, someDp); // not a context
// after
if (ReferenceEquals(freezable.InheritanceContext, otherOwner))
    freezable.RemoveInheritanceContext(otherOwner, someDp);
Defensive patterns

Strategy: try-catch

Validate before calling

if (ReferenceEquals(freezable.InheritanceContext, context)) freezable.RemoveInheritanceContext(context, dp);

Try / catch

try { freezable.RemoveInheritanceContext(owner, dp); }
catch (ArgumentException ex) { log.Warn("Context was not registered on this Freezable", ex); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/0ee387c1ee152201. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Freezable.cs:1088

                    {
                        ++deadRefs;
                    }
                }

                if (index != -1)
                {
                    Debug.Assert(!failed);

                    list.RemoveAt(index);
                }

                PruneContexts(list, deadRefs);
            }

            // Make sure we actually removed something - if not throw an exception
            if (failed)
            {
                throw new ArgumentException(SR.Freezable_NotAContext, nameof(context));
            }
        }

        /// <summary>
        /// Removes the single piece of contextual information that we have and updates all flags
        /// accordingly.
        /// </summary>
        private void RemoveSingletonContext()
        {
            Debug.Assert(Freezable_UsingSingletonContext);
            Debug.Assert(SingletonContext != null);

            if (HasHandlers)
            {
                _contextStorage = ((HandlerContextStorage)_contextStorage)._handlerStorage;
            }
            else
            {

View on GitHub (pinned to 81131a70a4)