AvaloniaUI/Avalonia · error · ArgumentNullException
glyphRun
Error message
glyphRun
What it means
Thrown by PlatformDrawingContext.DrawGlyphRun when the GlyphRun argument is null. The drawing context dereferences glyphRun.PlatformImpl immediately, so a null reference cannot proceed; the explicit guard produces a clear ArgumentNullException instead of a NullReferenceException deeper in the stack.
Source
Thrown at src/Avalonia.Base/Media/PlatformDrawingContext.cs:60
_impl.DrawBitmap(source.Item, opacity, sourceRect, destRect);
public override void Custom(ICustomDrawOperation custom)
{
using var immediateDrawingContext = new ImmediateDrawingContext(_impl, false);
try
{
custom.Render(immediateDrawingContext);
}
catch (Exception e)
{
Logger.TryGet(LogEventLevel.Error, LogArea.Visual)
?.Log(custom, $"Exception in {custom.GetType().Name}.{nameof(ICustomDrawOperation.Render)} {{0}}", e);
}
}
public override void DrawGlyphRun(IBrush? foreground, GlyphRun glyphRun)
{
_ = glyphRun ?? throw new ArgumentNullException(nameof(glyphRun));
if (foreground != null)
_impl.DrawGlyphRun(foreground, glyphRun.PlatformImpl.Item);
}
protected override void PushClipCore(RoundedRect rect) => _impl.PushClip(rect);
protected override void PushClipCore(Rect rect) => _impl.PushClip(rect);
protected override void PushGeometryClipCore(Geometry clip) =>
_impl.PushGeometryClip(clip.PlatformImpl ?? throw new ArgumentException());
protected override void PushOpacityCore(double opacity) =>
_impl.PushOpacity(opacity, null);
protected override void PushOpacityMaskCore(IBrush mask, Rect bounds) =>
_impl.PushOpacityMask(mask, bounds);
View on GitHub (pinned to 11c5427268)
Solutions
- Pass null for the foreground brush to draw nothing, or simply skip the DrawGlyphRun call when there is no glyph run.
- Ensure the GlyphRun is constructed from a valid GlyphTypeface and non-empty metrics before drawing.
- Guard: if (glyphRun is not null) context.DrawGlyphRun(foreground, glyphRun);
Example fix
// before
context.DrawGlyphRun(foreground, null);
// after
if (glyphRun is not null)
context.DrawGlyphRun(foreground, glyphRun); Defensive patterns
Strategy: validation
Validate before calling
if (glyphRun is not null)
context.DrawGlyphRun(foreground, glyphRun); Type guard
static bool HasGlyphRun(GlyphRun? run) => run is not null;
Prevention
- Skip the draw call when there is no glyph run rather than passing null.
- Construct GlyphRun only from a valid GlyphTypeface and non-empty metrics.
When it happens
Trigger: Calling DrawGlyphRun(foreground, null) directly, or passing a GlyphRun field/property that was never constructed.
Common situations: Rendering text where the GlyphRun was built from a null glyph typeface or zero-length text and ended up null; conditional text rendering that passes null when 'no text' was intended.
Related errors
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/00bb4afce5d686eb.
Report an issue: GitHub.