AvaloniaUI/Avalonia · error · ArgumentNullException
glyphRun
Error message
glyphRun
What it means
DrawGlyphRun throws ArgumentNullException(nameof(glyphRun)) when the IImmutableGlyphRunReference is null. The context dereferences glyphRun.GlyphRun, so a null reference is rejected up front.
Source
Thrown at src/Avalonia.Base/Media/ImmediateDrawingContext.cs:190
return;
}
var originX = center.X - radiusX;
var originY = center.Y - radiusY;
var width = radiusX * 2;
var height = radiusY * 2;
PlatformImpl.DrawEllipse(brush, pen, new Rect(originX, originY, width, height));
}
/// <summary>
/// Draws a glyph run.
/// </summary>
/// <param name="foreground">The foreground brush.</param>
/// <param name="glyphRun">The glyph run.</param>
public void DrawGlyphRun(IImmutableBrush foreground, IImmutableGlyphRunReference glyphRun)
{
_ = glyphRun ?? throw new ArgumentNullException(nameof(glyphRun));
if (glyphRun.GlyphRun != null)
PlatformImpl.DrawGlyphRun(foreground, glyphRun.GlyphRun.Item);
}
/// <summary>
/// Draws a filled rectangle.
/// </summary>
/// <param name="brush">The brush.</param>
/// <param name="rect">The rectangle bounds.</param>
/// <param name="cornerRadius">The corner radius.</param>
public void FillRectangle(IImmutableBrush brush, Rect rect, float cornerRadius = 0.0f)
{
DrawRectangle(brush, null, rect, cornerRadius, cornerRadius);
}
public readonly record struct PushedState : IDisposable
{
private readonly int _level;View on GitHub (pinned to 11c5427268)
Solutions
- Null-check the glyph run before drawing.
- Skip the draw call when the text run is empty.
- Ensure GlyphRun objects are built before being wrapped as references.
Example fix
// before
context.DrawGlyphRun(foreground, glyphRunRef); // may be null
// after
if (glyphRunRef is not null)
context.DrawGlyphRun(foreground, glyphRunRef); Defensive patterns
Strategy: validation
Validate before calling
if (glyphRun is null) return; context.DrawGlyphRun(foreground, glyphRun);
Prevention
- Null-check glyph runs before drawing.
- Skip drawing for empty text runs.
- Build GlyphRun instances before wrapping them as references.
When it happens
Trigger: Calling context.DrawGlyphRun(foreground, null) — e.g. a text rendering path that produced no glyph run or passed a reference before it was built.
Common situations: Custom text controls drawing before the glyph run is computed; formatted text with no characters yielding null; partial initialization of a GlyphRunReference.
Related errors
- source
- ImmediateDrawingContext
- DrawingContext
- Wrong Push/Pop state order
- Transform container stack is non-empty
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/356ea698322e8383.
Report an issue: GitHub.