AvaloniaUI/Avalonia · error · InvalidOperationException
Object is not yet attached to the compositor
Error message
Object is not yet attached to the compositor
What it means
Thrown by CompositionCustomVisualHandler.VerifyAccess when the handler's _host is null — i.e. the handler is not yet attached to a ServerCompositionCustomVisual. Most protected members (EffectiveSize, CompositionNow, SendNextRender) call VerifyAccess first, so any access before the handler is attached to a CompositionCustomVisual fails fast.
Source
Thrown at src/Avalonia.Base/Rendering/Composition/CompositionCustomVisualHandler.cs:47
_inRender = true;
_currentTransformedClip = currentTransformedClip;
_currentTransform = drawingContext.CurrentTransform;
try
{
OnRender(drawingContext);
}
finally
{
_inRender = false;
}
}
public abstract void OnRender(ImmediateDrawingContext drawingContext);
void VerifyAccess()
{
if (_host == null)
throw new InvalidOperationException("Object is not yet attached to the compositor");
_host.Compositor.VerifyAccess();
}
void VerifyInRender()
{
VerifyAccess();
if (!_inRender)
throw new InvalidOperationException("This API is only available from OnRender");
}
protected Vector EffectiveSize
{
get
{
VerifyAccess();
return _host!.Size;
}
}View on GitHub (pinned to 11c5427268)
Solutions
- Access handler members only from OnRender/OnMessage (which run after attachment), not from the constructor or external code.
- In tests, attach the handler to a real or mock ServerCompositionCustomVisual before exercising it.
- Do not hold and later use a handler reference from a detached visual.
Example fix
// before var handler = new MyHandler(); var size = handler.EffectiveSize; // throws: not attached // after var visual = compositor.CreateCustomVisual(handler); // attaches handler // size is only read inside handler.OnRender
Defensive patterns
Strategy: validation
Type guard
static bool IsAttached(CompositionCustomVisualHandler h) => h is not null; // real check: only call from OnRender/OnMessage
Prevention
- Access handler members only from OnRender/OnMessage (post-attach).
- Attach the handler via compositor.CreateCustomVisual(handler) before any use.
- In tests, wire the handler to a real/mock ServerCompositionCustomVisual first.
When it happens
Trigger: Reading EffectiveSize/CompositionNow or calling rendering APIs on a CompositionCustomVisualHandler instance that was never attached, or before Attach was called (e.g. constructing a handler and querying it directly without going through CompositionCustomVisual).
Common situations: Instantiating a handler standalone in a unit test or helper and calling its members; using a handler detached from its visual; accessing handler state in the constructor before the compositor attaches it.
Related errors
- This API is only available from OnRender
- Animation has no key frames
- Visual was invalidated during the render pass
- Unable to obtain ANativeWindow
- TopLevel.InternalView was not expected to be null.
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/a4907ffc3b0ffd5e.
Report an issue: GitHub.