c# - Prism的DelegateCommand内存泄漏

标签 c# wpf memory-leaks prism

使用此代码,您可以 100% 重现我的问题。我创建了一个“父”ViewModel:

using Prism.Commands;
using Prism.Mvvm;
using System.Collections.ObjectModel;

namespace WpfApplication1 {
    public class ViewModel : BindableBase {

        public ObservableCollection<ChildViewModel> Items { get; set; }

        public DelegateCommand<ChildViewModel> CloseItem { get; set; }

        public ViewModel() {
            CloseItem = new DelegateCommand<ChildViewModel>(OnItemClose);

            Items = new ObservableCollection<ChildViewModel>(new[] {
                new ChildViewModel { Name = "asdf" },
                new ChildViewModel { Name = "zxcv" },
                new ChildViewModel { Name = "qwer" },
                new ChildViewModel { Name = "fdgz" },
                new ChildViewModel { Name = "hgjkghk" }
            });
        }

        private void OnItemClose(ChildViewModel obj) {
            Items.Remove(obj);
        }
    }
}

其中包含 ChildViewModel 实例的 ObservableCollection:

using Prism.Mvvm;

namespace WpfApplication1 {
    public class ChildViewModel : BindableBase {

        public string Name { get; set; }
        protected byte[] leakArr = new byte[1024 * 1024 * 10];

    }
}

如您所见,我在每个 ChildViewModel 中声明了一个 10mb 的数组。现在的 View :

public partial class MainWindow : Window {
    public MainWindow() {
        InitializeComponent();

        DataContext = new ViewModel();
    }
}

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <TabControl ItemsSource="{Binding Items}">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <DockPanel>
                    <TextBlock Text="{Binding Path=Name}" DockPanel.Dock="Left" Margin="2,3,0,2" />
                    <Button DockPanel.Dock="Right" BorderThickness="0" Background="Transparent" 
                            Margin="10,2,2,2" 
                            Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabControl}}, Path=DataContext.CloseItem}" 
                            CommandParameter="{Binding}">
                        <Button.Content>
                            <TextBlock FontWeight="Bold" Text="X" />
                        </Button.Content>
                    </Button>
                </DockPanel>
            </DataTemplate>
        </TabControl.ItemTemplate>
    </TabControl>
</Window>

查看 VS2015 内存诊断工具,我可以看到,即使从 Items 集合中删除每个 ChildViewModel 后,它仍然存在于内存中,并带有一个对象(对于每个虚拟机)仍然保留对它的引用 - 一个 Button 控件。有趣的是,如果我将 Button 的命令声明替换为:

Click="Button_Click"

private void Button_Click(object sender, RoutedEventArgs e) {
            (DataContext as ViewModel).CloseItem.Execute(((sender as Button).DataContext as ChildViewModel));
        }

没有内存泄漏 - 这意味着调试工具不会显示任何已处置的 ChildViewModel 实例。我不知道这是否是 Prism 特有的问题,或者是否是某种 wpf 怪癖。

我正在使用最新版本的 .net 和 prism 库。

最佳答案

看来我找到了这个问题的解决方案(至少在我的应用程序中) - 我停止直接绑定(bind)到 ChildViewModel 实例,而是绑定(bind) CommandParameter > 到 ChildViewModelName 属性。然后,在 ViewModel 中,我只是浏览 Items 集合并删除具有匹配属性值的项目。现在 VS 诊断工具没有显示任何应该被 GC 却没有被 GC 的对象,因此内存泄漏消失了。

Prism团队正在分析这个问题:https://github.com/PrismLibrary/Prism/issues/345

关于c# - Prism的DelegateCommand内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34188671/

相关文章:

c# - Xamarin Forms MVVM更新不适用于UWP

c# - 使用 HwndSource 在 Win32 应用程序中托管 WPF UserControl

wpf - WPF 4 DataGrid:将行号放入RowHeader

c++ - 在 C++ 中检查内存泄漏的最佳方法是什么?

c - C 中读取文件到链表的内存泄漏

c++ - 删除在另一个函数中分配的内存?

c# - .NET 中是否有 "gettimeofday"的等价物

c# - 在 ASP.NET/C#/IIS 站点上实现守护进程?

c# - 拆分字符串并分别检查每个字符串

.net - 使用起点 X/Y 和起点 + 扫掠角在 ArcSegment 中获取终点