dotnet/wpf · error · ArgumentNullException

ArgumentNullException: child

Error message

ArgumentNullException: child

What it means

TextFlow.OnChildDesiredSizeChanged throws ArgumentNullException when the child parameter is null. The override forwards the child to OnChildUIElementChanged, which requires a real UIElement to re-measure.

Solutions

  1. Verify the child UIElement is non-null and still attached before notifying size changes
  2. Skip the notification if the child reference became null (element was removed)
  3. Fix the upstream code that produced a null child (removed/disposed element)

Example fix

// before
flow.OnChildDesiredSizeChanged(GetChild()); // may return null

// after
var child = GetChild();
if (child != null) flow.OnChildDesiredSizeChanged(child);
Defensive patterns

Strategy: type-guard

Validate before calling

if (child != null) flow.OnChildDesiredSizeChanged(child);

Type guard

bool isAttachedChild(UIElement c) => c != null && c.Parent != null;

Try / catch

try { flow.OnChildDesiredSizeChanged(child); } catch (ArgumentNullException) { /* child was detached/nulled; skip */ }

Prevention

When it happens

Trigger: Passing null to OnChildDesiredSizeChanged directly or via layout plumbing that was handed a null child reference.

Common situations: Custom layout code invoking parent invalidation APIs with a stale/cleared child reference; framework interop where a child was removed before notifying the parent.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextFlow.cs:905

                            {
                                InvalidateMeasure();
                                InvalidateVisual(); //ensure re-rendering
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Notification that is called by Measure of a child when it ends up with 
        /// different desired size for the child. 
        /// </summary>
        protected sealed override void OnChildDesiredSizeChanged(UIElement child)
        {
            if (child == null)
            {
                throw new ArgumentNullException("child");
            }

            OnChildUIElementChanged(child);
        }

        /// <summary>
        /// This method is called from during property invalidation time. If the FrameworkElement has a child on which
        /// some property was invalidated and the property was marked as AffectsParentMeasure or AffectsParentArrange
        /// during registration, this method is invoked to let a FrameworkElement know which particualr child must be
        /// remeasured if the FrameworkElement wants to do partial (incremental) update of layout.
        /// <para/>
        /// Olny advanced FrameworkElement, which implement incremental update should override this method. Since
        /// Panel always gets InvalidateMeasure or InvalidateArrange called in this situation, it ensures that
        /// the FrameworkElement will be re-measured and/or re-arranged. Only if the FrameworkElement wants to implement a performance
        /// optimization and avoid calling Measure/Arrange on all children, it should override this method and
        /// store the info about invalidated children, to use subsequently in the FrameworkElement's MeasureOverride/ArrangeOverride
        /// implementations.
        /// <para/>

View on GitHub (pinned to 81131a70a4)