{"record":{"id":"86159067bd13f317","repo":"AvaloniaUI/Avalonia","slug":"attempt-to-call-invalidatemeasure-on-wrong-layoutm","errorCode":null,"errorMessage":"Attempt to call InvalidateMeasure on wrong LayoutManager.","messagePattern":"Attempt to call InvalidateMeasure on wrong LayoutManager\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Layout/LayoutManager.cs","lineNumber":68,"sourceCode":"\n            if (_disposed)\n            {\n                return;\n            }\n\n            if (!control.IsAttachedToVisualTree)\n            {\n#if DEBUG\n                throw new AvaloniaInternalException(\n                    \"LayoutManager.InvalidateMeasure called on a control that is detached from the visual tree.\");\n#else\n                return;\n#endif\n            }\n\n            if (control.GetLayoutRoot() != _owner)\n            {\n                throw new ArgumentException(\"Attempt to call InvalidateMeasure on wrong LayoutManager.\");\n            }\n\n            _toMeasure.Enqueue(control);\n            QueueLayoutPass();\n        }\n\n        /// <inheritdoc/>\n        public virtual void InvalidateArrange(Layoutable control)\n        {\n            control = control ?? throw new ArgumentNullException(nameof(control));\n            Dispatcher.UIThread.VerifyAccess();\n\n            if (_disposed)\n            {\n                return;\n            }\n\n            if (!control.IsAttachedToVisualTree)","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Layout/LayoutManager.cs#L50-L86","documentation":"InvalidateMeasure throws when a control whose layout root does not match the LayoutManager's owner is passed for invalidation. Each LayoutManager is scoped to one layout root; calling it on a control that belongs to a different visual tree root indicates a cross-tree misuse of the layout invalidation API. The guard fires after confirming the control IS attached to a visual tree, but is rooted elsewhere.","triggerScenarios":"Calling layoutManager.InvalidateMeasure(control) where control.GetLayoutRoot() != layoutManager._owner. Can happen when a control is reparented between windows, or when a shared/global LayoutManager reference is used instead of the control's own layout root's manager.","commonSituations":"Reparenting a control between different top-level windows without going through proper removal/add. Holding a stale LayoutManager reference after the control moved. A control moved to a popup or overlay with its own layout root while a previous manager still tries to invalidate it.","solutions":["Use the control's own layout root manager — call control.GetLayoutRoot().LayoutManager or let the control invalidate itself via its Layoutable.InvalidateMeasure() method.","Ensure the control is properly removed from its old visual tree before adding it to a new one.","Avoid caching LayoutManager references across reparenting operations."],"exampleFix":"// before\n_someSharedLayoutManager.InvalidateMeasure(myControl);\n\n// after\nmyControl.InvalidateMeasure(); // routes through the correct manager automatically","handlingStrategy":"validation","validationCode":"// Always invalidate through the control itself rather than a cached manager\n// control.InvalidateMeasure() routes to the correct manager automatically.\n// If you must use a manager directly, verify ownership:\nif (control.GetLayoutRoot() == layoutManagerOwner)\n    layoutManager.InvalidateMeasure(control);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer control.InvalidateMeasure() over direct LayoutManager calls.","Never cache LayoutManager references across reparenting operations.","After reparenting a control, ensure old invalidation queues no longer target it."],"tags":["layout","layout-manager","visual-tree","reparenting"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}