Apparent memory leak in DockingManager
还没有人认领这个 Issue。
评估
调研方向
首先阅读 DockingManager 代码中的 CollectLayoutItemsDeleted、RemoveDocumentLayoutItem 和 LayoutItem.Detach()。重现报告中添加并关闭 LayoutAnchorable 项目的循环,然后检查已关闭的项目是否从 _layoutItems 和 _logicalChildren 中移除并可以被回收。修复泄漏且不破坏文档清理即为完成。
由索引模型根据 Issue 内容生成。
描述
An application is set up with a DockingManager in xaml like
<UserControl
x:Class="Test.MainDockingContentContainer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xcad="clr-namespace:Xceed.Wpf.AvalonDock;assembly=Xceed.Wpf.AvalonDock"
xmlns:layout="clr-namespace:Xceed.Wpf.AvalonDock.Layout;assembly=Xceed.Wpf.AvalonDock">
<Grid>
<xcad:DockingManager x:Name="DockManager">
<layout:LayoutRoot>
<layout:LayoutPanel Orientation="Horizontal">
<layout:LayoutAnchorablePaneGroup Orientation="Vertical">
<layout:LayoutAnchorablePane x:Name="A" DockHeight="0.7*"/>
<layout:LayoutAnchorablePane x:Name="B" DockHeight="0.3*" />
</layout:LayoutAnchorablePaneGroup>
</layout:LayoutPanel>
</layout:LayoutRoot>
</xcad:DockingManager>
</Grid>
</UserControl>
and code-behind which is equivalent to
partial MainDockingContentContainer()
{
InitializeComponent()
A.Name = "A";
B.Name = "B";
}
this gets used to add/remove content in code, essentially like this:
var testPane = mainDock.DockManager.Layout.Descendents().OfType<LayoutAnchorablePane>().Where( l => l.Name == "A" ).FirstOrDefault();
for( var i = 0 ; i < 20 ; i++ )
{
var testWin = new LayoutAnchorable { Content = new System.Windows.Controls.TextBlock { Text = "content" } };
testPane.Children.Add( testWin );
testWin.Show();
testWin.Close();
}
The result of this is that after this loop ran testPane.Children is empty as expected, but
DockingManager._layoutItemsstill contains 20LayoutAnchorableItem- that LayoutAnchorableItem's
LayoutElementstill is set totestWin(though itsRootis null), and itsModelstill set to theTextBlock - both can never get garbage collected so in an actual application causes severe leaks, plus
_layoutItemsgrows bigger and bigger - similar issue exists for
DockingManager._logicalChildren
In other words: even though the LayoutAnchorable was closed indicating the caller doesn't need it anymore, it remains in memory forever. I first thought perhaps what I'm doing is not intended usage (but if not Close, what else would have to be used?) but on further inspection I think it's an oversight: core issue seems to be that the LayoutItem.Detach() method (which is responsible for clearing LayoutElement and Model) never gets called. I also see no code in DockingManager which does that, whereas that should probably be the place where it should happen; also because there is already CollectLayoutItemsDeleted and and which does
LayoutItem[] array = _layoutItems.Where( ( LayoutItem item ) => item.LayoutElement.Root != Layout ).ToArray();
for( int i = 0 ; i < array.Count() ; i++ )
{
LayoutItem layoutItem = array[ i ];
RemoveDocumentLayoutItem( layoutItem.LayoutElement as LayoutDocument );
}
and RemoveDocumentLayoutItem is
LayoutItem layoutItem = _layoutItems.FirstOrDefault( ( LayoutItem item ) => item.LayoutElement == contentToRemove );
if( layoutItem != null )
{
layoutItem._ClearDefaultBindings();
layoutItem.Detach();
_layoutItems.Remove( layoutItem );
if( contentToRemove != null && contentToRemove.Content != null && contentToRemove.Content is UIElement )
{
InternalRemoveLogicalChild( contentToRemove.Content );
}
}
So I guess what is missing here is there is actually nothing specific to LayoutDocument in RemoveDocumentLayoutItem, it just looks up LayoutItem, and simply removing as LayoutDocument fixes the issue?
- 主要语言
- C#
- 星标
- 4.2k
- 派生
- 907
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
xceedsoftware/wpftoolkit 的其他 Issue
-
难度 3/5 1-2 天 新手友好度 62/100
xceedsoftware/wpftoolkit#1811 · 5 条评论 ·
-
难度 4/5 3-5 天 新手友好度 35/100
xceedsoftware/wpftoolkit#1808 · 1 条评论 ·
-
难度 3/5 1-2 天 新手友好度 48/100
xceedsoftware/wpftoolkit#1807 · 2 条评论 ·
-
难度 3/5 1-2 天 新手友好度 35/100
xceedsoftware/wpftoolkit#1806 · 2 条评论 ·
-
难度 1/5 1 小时以内 新手友好度 45/100
xceedsoftware/wpftoolkit#1804 · 1 条评论 ·
查看 xceedsoftware/wpftoolkit 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 76/100
-
area:repo elsa 3 prio low triaged type:bug
难度 2/5 1-3 小时 新手友好度 72/100
elsa-workflows/elsa-core#9258 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 90/100
iokode/OpinionatedFramework#140 ·
-
accessibility effort: small enhancement
难度 2/5 1-3 小时 新手友好度 78/100
pedersebastian/CustomBossChallenge#185 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100