11

我目前有一个奇怪的 WPF TreeView 内存泄漏。当我在 TreeView 中选择一个项目时,对应的绑定 ViewModel 强烈地保存在 TreeView EffectiveValueEntry[] 集合中。问题是当 ViewModel 从其父集合中删除时它不会被释放。

这是重现该问题的简单代码:

主窗口.xaml

using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls.Primitives;

namespace TreeViewMemoryLeak
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }

        public ObservableCollection<Entry> Entries
        {
            get
            {
                if (entries == null)
                {
                    entries = new ObservableCollection<Entry>() { new Entry() { DisplayName = "First Entry" } };
                }
                return entries;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e) { entries.Clear(); }

        private ObservableCollection<Entry> entries;

    }

    public class Entry : DependencyObject
    {
        public string DisplayName { get; set; }
    }
}

主窗口.xaml.cs

<Window x:Class="TreeViewMemoryLeak.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TreeViewMemoryLeak"
    Title="MainWindow" Height="350" Width="250">

    <Window.Resources>
        <DataTemplate DataType="{x:Type local:Entry}">
            <TextBlock Text="{Binding DisplayName}" />
        </DataTemplate>
    </Window.Resources>

    <StackPanel>
        <Button Content="delete item" Click="Button_Click" Grid.Row="0" Margin="10"/>
        <TreeView x:Name="treeView" ItemsSource="{Binding Entries}" Grid.Row="1" Margin="10" BorderBrush="Black" BorderThickness="1" />
    </StackPanel>

</Window>

重现问题

选择项目,然后单击按钮以清除 ObservableCollection。现在检查 TreeView 控件上的 EffectiveValueEntry[]:ViewModel 仍然存在并且没有标记为垃圾回收。

4

3 回答 3

3

好吧,我终于想出了一个相当暴力的解决方案。在删除 TreeView 中的最后一个对象时,我自己从 EffectiveValues 集合中删除了引用。这可能是矫枉过正,但至少,它的工作原理。

public class MyTreeView : TreeView
{
    protected override void OnSelectedItemChanged(RoutedPropertyChangedEventArgs<object> e)
    {
        base.OnSelectedItemChanged(e);

        if (Items.Count == 0)
        {
            var lastObjectDeleted = e.OldValue;
            if (lastObjectDeleted != null)
            {
                var effectiveValues = EffectiveValuesGetMethod.Invoke(this, null) as Array;
                if (effectiveValues == null)
                    throw new InvalidOperationException();

                bool foundEntry = false;
                int index = 0;
                foreach (var effectiveValueEntry in effectiveValues)
                {
                    var value = EffectiveValueEntryValueGetMethod.Invoke(effectiveValueEntry, null);
                    if (value == lastObjectDeleted)
                    {
                        foundEntry = true;
                        break;
                    }
                    index++;
                }

                if (foundEntry)
                {
                    effectiveValues.SetValue(null, index);
                }
            }
        }
    }

    protected MethodInfo EffectiveValueEntryValueGetMethod
    {
        get
        {
            if (effectiveValueEntryValueGetMethod == null)
            {
                var effectiveValueEntryType = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes()).Where(t => t.Name == "EffectiveValueEntry").FirstOrDefault();
                if (effectiveValueEntryType == null)
                    throw new InvalidOperationException();

                var effectiveValueEntryValuePropertyInfo = effectiveValueEntryType.GetProperty("Value", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Instance);
                if (effectiveValueEntryValuePropertyInfo == null)
                    throw new InvalidOperationException();

                effectiveValueEntryValueGetMethod = effectiveValueEntryValuePropertyInfo.GetGetMethod(nonPublic: true);
                if (effectiveValueEntryValueGetMethod == null)
                    throw new InvalidOperationException();

            }
            return effectiveValueEntryValueGetMethod;
        }
    }

    protected MethodInfo EffectiveValuesGetMethod
    {
        get
        {
            if (effectiveValuesGetMethod == null)
            {
                var dependencyObjectType = typeof(DependencyObject);
                var effectiveValuesPropertyInfo = dependencyObjectType.GetProperty("EffectiveValues", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Instance);
                if (effectiveValuesPropertyInfo == null)
                    throw new InvalidOperationException();

                effectiveValuesGetMethod = effectiveValuesPropertyInfo.GetGetMethod(nonPublic: true);
                if (effectiveValuesGetMethod == null)
                    throw new InvalidOperationException();
            }
            return effectiveValuesGetMethod;
        }
    }

    #region Private fields
    private MethodInfo effectiveValueEntryValueGetMethod;
    private MethodInfo effectiveValuesGetMethod;
    #endregion
}
于 2012-10-04T11:22:01.830 回答
1

这是因为您将树视图与OneTime模式绑定,所以您的收藏被“快照”了。就像声明的那样:

更新:

EffectiveValueEntry是关于如何DependencyObjects存储它们的值DependencyProperties。只要 treeView 选择了项目,这个集合就会保存对象。一旦您选择其他内容,集合就会更新。

于 2012-10-04T08:11:32.627 回答
1

我遇到了同样的问题,并使用其中一种解决方案link(由 Tom Goff 发布)解决了它。请执行下列操作:

ClearSelection(this.treeView);
this.treeView.SelectedValuePath = ".";
this.treeView.ClearValue(TreeView.SelectedValuePathProperty);
this.treeView.ItemsSource = null;

...

public static void ClearSelection(TreeView treeView)
{
    if (treeView != null)
        ClearSelection(treeView.Items, treeView.ItemContainerGenerator);
}

private static void ClearSelection(ItemCollection collection, ItemContainerGenerator generator)
{
    if ((collection != null) && (generator != null))
    {
        for (int i = 0; i < collection.Count; i++)
        {
            TreeViewItem treeViewItem = generator.ContainerFromIndex(i) as TreeViewItem;
            if (treeViewItem != null)
            {
                ClearSelection(treeViewItem.Items, treeViewItem.ItemContainerGenerator);
                treeViewItem.IsSelected = false;
            }
        }
    }
}
于 2013-03-01T23:32:14.837 回答