AvaloniaUI/Avalonia · error · InvalidOperationException
Invalid size returned for Measure.
Error message
Invalid size returned for Measure.
What it means
Measure throws after MeasureCore returns a Size whose Width or Height is negative or non-finite (NaN/Infinity). This is the contract check on the OUTPUT of measure: subclasses overriding MeasureCore must return a valid, non-negative, finite desired size. Infinity or NaN in DesiredSize would corrupt every parent's measure math.
Source
Thrown at src/Avalonia.Base/Layout/Layoutable.cs:396
var previousDesiredSize = DesiredSize;
var desiredSize = default(Size);
IsMeasureValid = true;
try
{
_measuring = true;
desiredSize = MeasureCore(availableSize);
}
finally
{
_measuring = false;
}
if (IsInvalidSize(desiredSize))
{
throw new InvalidOperationException("Invalid size returned for Measure.");
}
DesiredSize = desiredSize;
_previousMeasure = availableSize;
Logger.TryGet(LogEventLevel.Verbose, LogArea.Layout)?.Log(this, "Measure requested {DesiredSize}", DesiredSize);
if (DesiredSize != previousDesiredSize)
{
this.GetVisualParent<Layoutable>()?.ChildDesiredSizeChanged(this);
}
}
}
/// <summary>
/// Arranges the control and its children.
/// </summary>
/// <param name="rect">The control's new bounds.</param>View on GitHub (pinned to 11c5427268)
Solutions
- In the MeasureCore/MeasureOverride override, ensure the returned Size has non-negative, finite Width and Height.
- Clamp computed values: Math.Max(0, value) and guard against NaN/Infinity before returning.
- If a child measure failed, fall back to a sensible default size rather than propagating an invalid result.
Example fix
// before
protected override Size MeasureCore(Size availableSize)
{
return new Size(DesiredSize.Width / someDivisorThatIsZero, height);
}
// after
protected override Size MeasureCore(Size availableSize)
{
var w = someDivisor != 0 ? DesiredSize.Width / someDivisor : 0;
return new Size(Math.Max(0, w), height);
} Defensive patterns
Strategy: validation
Validate before calling
// Inside a MeasureCore/MeasureOverride override, validate the return value:
static Size Valid(Size s) => new Size(
double.IsNaN(s.Width) || s.Width < 0 ? 0 : s.Width,
double.IsNaN(s.Height) || s.Height < 0 ? 0 : s.Height);
return Valid(computedDesiredSize); Prevention
- Always clamp MeasureCore/MeasureOverride return values to non-negative finite sizes.
- Guard divisions that could produce NaN/Infinity before returning.
- Unit-test custom controls' measure output with edge-case available sizes.
When it happens
Trigger: A custom Layoutable subclass whose MeasureCore override returns a Size with a negative component, NaN, or Infinity. Can also indicate arithmetic in the override that divided by zero or returned an uninitialized Size.
Common situations: Writing a custom control/panel and returning `new Size(double.NaN, ...)` or a negative value from MeasureCore/MeasureOverride. Forgetting to clamp a computed desired size. Returning DesiredSize before it was set.
Related errors
- Cannot call Measure using a size with NaN values.
- The control isn't currently attached to a toplevel
- Invalid render scaling value {scaling}
- Attempt to call InvalidateMeasure on wrong LayoutManager.
- Attempt to call InvalidateArrange on wrong LayoutManager.
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/aed201449d6c0016.
Report an issue: GitHub.