Apparent memory leak in DockingManager

未关闭 适合新手
#1,812 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
72/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
csharp
领域
desktop

调研方向

首先阅读 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._layoutItems still contains 20 LayoutAnchorableItem
  • that LayoutAnchorableItem's LayoutElement still is set to testWin (though its Root is null), and its Model still set to the TextBlock
  • both can never get garbage collected so in an actual application causes severe leaks, plus _layoutItems grows 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,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

xceedsoftware/wpftoolkit 的其他 Issue

查看 xceedsoftware/wpftoolkit 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。