{"record":{"id":"a4907ffc3b0ffd5e","repo":"AvaloniaUI/Avalonia","slug":"object-is-not-yet-attached-to-the-compositor","errorCode":null,"errorMessage":"Object is not yet attached to the compositor","messagePattern":"Object is not yet attached to the compositor","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Rendering/Composition/CompositionCustomVisualHandler.cs","lineNumber":47,"sourceCode":"        _inRender = true;\n        _currentTransformedClip = currentTransformedClip;\n        _currentTransform = drawingContext.CurrentTransform;\n        try\n        {\n            OnRender(drawingContext);\n        }\n        finally\n        {\n            _inRender = false;\n        }\n    }\n\n    public abstract void OnRender(ImmediateDrawingContext drawingContext);\n\n    void VerifyAccess()\n    {\n        if (_host == null)\n            throw new InvalidOperationException(\"Object is not yet attached to the compositor\");\n        _host.Compositor.VerifyAccess();\n    }\n\n    void VerifyInRender()\n    {\n        VerifyAccess();\n        if (!_inRender)\n            throw new InvalidOperationException(\"This API is only available from OnRender\");\n    }\n\n    protected Vector EffectiveSize\n    {\n        get\n        {\n            VerifyAccess();\n            return _host!.Size;\n        }\n    }","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Rendering/Composition/CompositionCustomVisualHandler.cs#L29-L65","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before\nvar handler = new MyHandler();\nvar size = handler.EffectiveSize; // throws: not attached\n// after\nvar visual = compositor.CreateCustomVisual(handler); // attaches handler\n// size is only read inside handler.OnRender","handlingStrategy":"validation","validationCode":null,"typeGuard":"static bool IsAttached(CompositionCustomVisualHandler h) => h is not null; // real check: only call from OnRender/OnMessage","tryCatchPattern":null,"preventionTips":["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."],"tags":["rendering","composition","invalid-operation","lifecycle","custom-visual"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}