Gosha
Topic Author
Posts: 5
Joined: 27 Jul 2021, 16:06

Memory leak when binding commands

04 Aug 2021, 00:47

Hi guys! I ran into a problem and got stuck. I'm new to wpf and mvvm, maybe I'm missing something.

Binding to a command which is up the hierarchy causes memory leaks.
Yeah, I saw this topic. But in the context of commands, I don't know how to get around it.

I have a App UserControl, which consists only of ContentControl in the xaml. Depending on the App ViewModel, a menu is "inserted" into this ContentControl. Menus are also UserControls with their View Models (which are the fields of the App ViewModel). View Models of menus contains ViewModels of panels (also UserControls) and so on.
For clarity:
class ApplicationVM : public NotifyPropertyChangedBase
{
  public:
    FirstMenuVM _first_menu_vm;
    SecondMenuVM _second_menu_vm;
    ...
};
class FirstMenuVM : public NotifyPropertyChangedBase
{
  public:
    LeftPanelVM _left_panel_vm;
    TopPanelVM _top_panel_vm;
    ...
};
Everything is fine at this stage, but here's what's next.
I binding the UserControl command to the element of this UserControl. And since the command is a field of the View Model of this control, a memory leak occurs due to a known issue.
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="clr-namespace:D" x:Class="D.LibraryInlLeftPanel">
    <UserControl.Resources>
        <ItemsPanelTemplate x:Key="LibraryPanelTemplate">
            <WrapPanel/>
        </ItemsPanelTemplate>
    </UserControl.Resources>
        <ScrollViewer VerticalScrollBarVisibility="Auto">
            <ItemsControl DataContext="{Binding LibraryInlLeftPanelViewModel}" ItemsSource="{Binding ItemsObservableCollection }" ItemsPanel="{StaticResource LibraryPanelTemplate}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <d:ItemPreview ItemName="{Binding ItemName}" ItemSource="{Binding ItemSource}" Command="{Binding DataContext.Command, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type d:LibraryInlLeftPanel}}}" CommandParameter="{Binding}" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>
</UserControl>
Perhaps I need to somehow pass the datacontext from top to bottom in the hierarchy to avoid leaks? Or, in principle, I need to do architecturally differently?
Thanks for any help

Tags:
 
User avatar
sfernandez
Site Admin
Posts: 2336
Joined: 22 Dec 2011, 19:20

Re: Memory leak when binding commands

04 Aug 2021, 09:59

Hi, I don't see a cyclic reference here because you are not storing a reference to any parent element, just to the command object stored in the viewmodel. That shouldn't produce a memory leak.

Which objects are not correctly released in this scenario for you?
 
Gosha
Topic Author
Posts: 5
Joined: 27 Jul 2021, 16:06

Re: Memory leak when binding commands

04 Aug 2021, 18:33

Hey, thanks for the quick response!
The issue was in the logic of closing the panel.
<UserControl  
x:Name="FloatingWindow" 
x:Class="D.FloatingWindow">
    <UserControl.ContentTemplate>
        <DataTemplate>
            <Button>
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                                <ei:ChangePropertyAction TargetObject="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type d:FloatingWindow}}}" PropertyName="IsOpen" Value="False"/>                    
                     </i:EventTrigger>
                </i:Interaction.Triggers>
            </Button>
        </DataTemplate>
    </UserControl.ContentTemplate>
</UserControl>
I replaced the TargetObject with TargetName and there are no more memory leaks
<i:EventTrigger EventName="Click">
	<ei:ChangePropertyAction TargetName="FloatingWindow" PropertyName="IsOpen" Value="False"/>
</i:EventTrigger>
 
User avatar
sfernandez
Site Admin
Posts: 2336
Joined: 22 Dec 2011, 19:20

Re: Memory leak when binding commands

04 Aug 2021, 18:49

Ok, that makes more sense to me. Great to know you solved it.

Who is online

Users browsing this forum: No registered users and 1 guest