Prism的DelegateCommand内存泄漏

时间:2015-12-09 20:48:46

标签: c# wpf memory-leaks prism

使用此代码,您可以100%重现我的问题。我创建了一个父母'视图模型:

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);
        }
    }
}

其中包含ObservableCollectionChildViewModel个实例:

using Prism.Mvvm;

namespace WpfApplication1 {
    public class ChildViewModel : BindableBase {

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

    }
}

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

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内存诊断工具我可以看到,即使每次ChildViewModel已经从Items集合中删除了它,它仍然存在于内存中,其中一个对象(对于每个vm)仍然保持引用它 - 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实例。我不知道这是否是一个特定于棱镜的问题,或者它是否是某种wpf怪癖。

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

2 个答案:

答案 0 :(得分:2)

您是否检查过自定义ICommand实现是否具有相同的行为?

当我们因为相关错误而删除了弱引用时,我们可能会引入此内存泄漏。

请你在这里报告这个问题:

https://github.com/PrismLibrary/Prism/issues

我们会进行调查,我们会调查一下。

编辑:我很高兴听到这不是Prism问题:)

答案 1 :(得分:2)

我似乎找到了解决此问题的方法(至少在我的应用中) - 我停止直接绑定到ChildViewModel实例,而是将CommandParameter绑定到{ {1}} Name的属性。然后,在ChildViewModel中我只是通过Items集合并删除具有匹配属性值的项目。现在VS诊断工具没有显示任何应该已经GCed并且没有的对象,因此内存泄漏消失了。

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