{"record":{"id":"434467973b1f2719","repo":"dotnet/wpf","slug":"sr-treeloop","errorCode":null,"errorMessage":"SR.TreeLoop","messagePattern":"SR\\.TreeLoop","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/UIElement.cs","lineNumber":1707,"sourceCode":"                    UIElement uiElement = e as UIElement;\n                    ContentElement contentElement = null;\n                    UIElement3D uiElement3D = null;\n\n                    if (uiElement == null)\n                    {\n                        contentElement = e as ContentElement;\n\n                        if (contentElement == null)\n                        {\n                            uiElement3D = e as UIElement3D;\n                        }\n                    }\n\n                    // Protect against infinite loops by limiting the number of elements\n                    // that we will process.\n                    if (cElements++ > MAX_ELEMENTS_IN_ROUTE)\n                    {\n                        throw new InvalidOperationException(SR.TreeLoop);\n                    }\n\n                    // Allow the element to adjust source\n                    object newSource = null;\n                    if (uiElement != null)\n                    {\n                        newSource = uiElement.AdjustEventSource(args);\n                    }\n                    else if (contentElement != null)\n                    {\n                        newSource = contentElement.AdjustEventSource(args);\n                    }\n                    else if (uiElement3D != null)\n                    {\n                        newSource = uiElement3D.AdjustEventSource(args);\n                    }\n\n                    // Add changed source information to the route","sourceCodeStart":1689,"sourceCodeEnd":1725,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/UIElement.cs#L1689-L1725","documentation":"During event routing the number of elements processed exceeded MAX_ELEMENTS_IN_ROUTE, which the framework treats as a cyclic or runaway tree. InvalidOperationException(SR.TreeLoop) aborts the route. It protects against infinite loops caused by malformed visual/logical trees.","triggerScenarios":"Raising a routed event whose visual/logical tree path contains a cycle or an abnormally deep chain; reparenting elements during the route so traversal never terminates.","commonSituations":"Bugs in custom panels/controls that create parent-child loops (A is visual child of B and vice versa); changing the tree inside an event handler; pathological deep element hierarchies.","solutions":["Find and fix the cycle in the visual/logical tree (inspect Parent chains of elements involved in the route)","Avoid adding/removing or reparenting elements inside handlers while the event is routing","Reduce hierarchy depth or split the route","Debug with a tree dump (e.g. VisualTreeHelper.GetParent walking) before the event is raised"],"exampleFix":"// before\nchild.Parent = parent; parent.Children.Add(child); // possible cycle when child already ancestor\n// after\nif (!IsAncestorOf(child)) { parent.Children.Add(child); }","handlingStrategy":"try-catch","validationCode":"// Verify no parent cycle before raising\nbool HasCycle(DependencyObject start) { var seen = new HashSet<DependencyObject>(); var cur = start;\n  while (cur != null) { if (!seen.Add(cur)) return true; cur = VisualTreeHelper.GetParent(cur) ?? LogicalTreeHelper.GetParent(cur) as DependencyObject; } return false; }","typeGuard":null,"tryCatchPattern":"try { element.RaiseEvent(args); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"loop\") || ex.Message.Contains(\"TreeLoop\")) { log.Error(\"Possible visual-tree cycle during routing\", ex); }","preventionTips":["Never reparent elements inside routed-event handlers; defer with Dispatcher.BeginInvoke","Assert acyclic parent chains in debug builds when building custom panels","Keep visual tree depth reasonable and avoid accidental re-entrant RaiseEvent"],"tags":["wpf","routed-events","tree","infinite-loop"],"backgroundTag":"internal-invariant-violation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}