Apparent memory leak in DockingManager

Abierto Apto para principiantes
#1,812 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
72/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
csharp
Área
desktop

Línea de trabajo

Empieza leyendo CollectLayoutItemsDeleted, RemoveDocumentLayoutItem y LayoutItem.Detach() en el código de DockingManager. Reproduce el bucle notificado que añade y cierra elementos LayoutAnchorable; después, comprueba si los elementos cerrados se eliminan de _layoutItems y _logicalChildren y pueden ser recolectados. La tarea está terminada cuando se corrige la fuga sin afectar a la limpieza de documentos.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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?

Lenguaje dominante
C#
Estrellas
4.2k
Forks
907
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de xceedsoftware/wpftoolkit

Todos los issues de xceedsoftware/wpftoolkit

Issues similares

Más issues de C#

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.