{"record":{"id":"3dd8d04466372931","repo":"dotnet/wpf","slug":"sr-logicaltreeloop-frameworkelement","errorCode":null,"errorMessage":"SR.LogicalTreeLoop","messagePattern":"SR\\.LogicalTreeLoop","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs","lineNumber":1173,"sourceCode":"            FrameworkObject fo = startNode;\n            object value;\n            Style style;\n            FrameworkTemplate frameworkTemplate;\n            Style themeStyle;\n            int loopCount = 0;\n            bool hasParent = true;\n            inheritanceBehavior = InheritanceBehavior.Default;\n\n            while (hasParent)\n            {\n                Debug.Assert(startNode.IsValid || unlinkedParent != null,\n                              \"Don't call FindResource with a null fe/fce and unlinkedParent\");\n\n                if (loopCount > ContextLayoutManager.s_LayoutRecursionLimit)\n                {\n                    // We suspect a loop here because the loop count\n                    // has exceeded the MAX_TREE_DEPTH expected\n                    throw new InvalidOperationException(SR.LogicalTreeLoop);\n                }\n                else\n                {\n                    loopCount++;\n                }\n\n                // -------------------------------------------\n                //  Lookup ResourceDictionary on the current instance\n                // -------------------------------------------\n\n                style = null;\n                frameworkTemplate = null;\n                themeStyle = null;\n\n                if (fo.IsFE)\n                {\n                    FrameworkElement fe = fo.FE;\n","sourceCodeStart":1155,"sourceCodeEnd":1191,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs#L1155-L1191","documentation":"During FindResource resource lookup, WPF walks the logical tree upward. If the walk exceeds ContextLayoutManager.s_LayoutRecursionLimit (MAX_TREE_DEPTH), a loop in the logical tree is suspected and InvalidOperationException(SR.LogicalTreeLoop) is thrown. The library throws this to prevent stack overflow from a cyclic parent chain.","triggerScenarios":"Calling FindResource/TryFindResource (or resolving a DynamicResource) on an element whose logical-parent chain forms a cycle, or a tree deeper than the layout recursion limit.","commonSituations":"Accidentally setting LogicalParent in a loop in custom code; cyclic object graphs built programmatically; extremely deep generated trees (rare) exceeding the depth limit during resource resolution.","solutions":["Inspect and break the logical-tree cycle: ensure no element's parent chain loops back on itself","Set parents only via legal APIs (adding to a Panel, setting Content) rather than manually mutating parent references","Break very deep trees into separate trees or resolve resources explicitly via FindResource on a nearer element","For programmatic graphs, add your own depth guard before inserting nodes"],"exampleFix":"// before\nchild.Parent = parent; parent.Parent = child; // cycle\n// after\n// build a tree: only parent -> child links, no cycles\nparent.Children.Add(child);","handlingStrategy":"try-catch","validationCode":"// Walk parents yourself with a depth cap before calling FindResource\nint depth = 0; DependencyObject d = element;\nwhile ((d = LogicalTreeHelper.GetParent(d) ?? (d as FrameworkContentElement)?.Parent) != null) if (++depth > 200) throw new InvalidOperationException(\"logical tree loop\");","typeGuard":null,"tryCatchPattern":"try { var res = element.FindResource(key); }\ncatch (InvalidOperationException) { res = Application.Current.TryFindResource(key); }","preventionTips":["Never mutate Parent/parent references manually","Build trees via Content/Children APIs only","Add cycle detection in code that constructs logical trees programmatically"],"tags":["wpf","logical-tree","resource-lookup"],"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-22T01:17:13.364Z"}