2

我注意到我的代码中有内存泄漏我使用 Ants 内存分析器来查看内存泄漏来自我正在做的唯一操作是在我的应用程序中切换选项卡。

我已经从 WPF 扩展工具包 2.0.0.0 -> 2.5.0.0 升级,但这并没有解决我的问题

这是来自蚂蚁的图表 在此处输入图像描述

有一个collectionchangedeventhandler,它有7个实例,一切都位于一个obserablecollection<BaseViewerTabItem>

这是我的代码的一部分:

 public class ViewerBaseViewModel : ViewModelBase
    {
        protected bool IsViewerMode; // Used to identify if the image was loaded from a file to the viewer mode
        protected ImageManager _imageManager;
        private readonly IMessageBoxService _messageBoxService;
        private readonly ProgressIndicatorViewModel _progressIndicator;           
        protected ImageCollection CurrentImageCollection { get; set; }
        protected int _imageTabCounter = 1;

        private Dictionary<int, List<ImageViewerTabItem>> _groupedImages;

        public ObservableCollection<BaseViewerTabItem> OpenViewers { get; private set; }

如您所见,类 ImageViewerTabItem 注册到 OpenViewersCollectionChanged 事件并从中注销

public ImageViewerTabItem(RelayCommand<ImageViewerTabItem> closeTabCommand, RelayCommand<ImageViewerTabItem> duplicateTabcommand, RelayCommand<AttachableItem> attachTabCommand,
            RelayCommand<ImageViewerTabItem> detachTabCommand, RelayCommand ungroupAllCommand, RelayCommand groupAllCommand, ObservableCollection<BaseViewerTabItem> openViewers, Dictionary<int, List<ImageViewerTabItem>> groupedImages)
        {
            _closeTabCommand = closeTabCommand;
            _duplicateTabcommand = duplicateTabcommand;
            _attachTabCommand = attachTabCommand;
            _detachTabCommand = detachTabCommand;
            _ungroupAllCommand = ungroupAllCommand;
            _groupAllCommand = groupAllCommand;
            OpenViewers = openViewers;
            _groupedImages = groupedImages;
            OpenViewers.CollectionChanged += OpenViewersChanged;
        }

public void TearDown()
        {
            OnTabClosed();
            OpenViewers.CollectionChanged -= OpenViewersChanged;
            ImageContentViewModel.TearDown();
        }

这是我的 Xaml 代码

<Grid>
        <xcad:DockingManager DocumentsSource="{Binding OpenViewers}"
                             x:Name="MainTabControl"
                             LayoutItemTemplate="{StaticResource imageSelectionTemplate}"
                             ActiveContent="{Binding ActiveTabItem, Mode=TwoWay}"
                             DocumentHeaderTemplate="{StaticResource DocumentHeaderDataTemplate}"
                             DocumentTitleTemplate="{StaticResource DocumentTitleDataTemplate}" IsHitTestVisible="True" VerticalContentAlignment="Stretch" VerticalAlignment="Stretch">
            <xcad:LayoutRoot>
                <xcad:LayoutPanel Orientation="Horizontal" >
                    <xcad:LayoutDocumentPane />
                    </xcad:LayoutPanel>
                </xcad:LayoutRoot>

        </xcad:DockingManager>
    </Grid>

你能建议我的问题可能在哪里吗?
或者如何找到内存泄漏?
Avalon Dock 会不会有内存泄漏?

4

0 回答 0