Apparent memory leak in DockingManager

Aberta Para iniciantes
#1,812 0 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
2/5
Tempo estimado
1-3 horas
Facilidade para iniciantes
72/100
Tipo de issue
Bug
Clareza
Razoavelmente clara
Status de atividade
Ativa
Stack de tecnologia
csharp
Domínio
desktop

Direção de pesquisa

Comece lendo CollectLayoutItemsDeleted, RemoveDocumentLayoutItem e LayoutItem.Detach() no código de DockingManager. Reproduza o loop relatado que adiciona e fecha itens LayoutAnchorable e, em seguida, verifique se os itens fechados são removidos de _layoutItems e _logicalChildren e podem ser coletados. A tarefa estará concluída quando o vazamento for corrigido sem prejudicar a limpeza de documentos.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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?

Linguagem predominante
C#
Estrelas
4.2k
Forks
907
Métricas de merge de PRs
Nenhum PR com merge em 30d

Preparar o ambiente

Este projeto não oferece contêiner de desenvolvimento, Dockerfile nem guia de contribuição, então a configuração fica por sua conta: comece pelo README e veja nosso guia da primeira contribuição para os passos gerais.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de xceedsoftware/wpftoolkit

Todas as issues de xceedsoftware/wpftoolkit

Issues semelhantes

Mais issues de C#

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.