Memory leak when binding commands
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:
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.
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
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:
Code: Select all
class ApplicationVM : public NotifyPropertyChangedBase
{
public:
FirstMenuVM _first_menu_vm;
SecondMenuVM _second_menu_vm;
...
};
Code: Select all
class FirstMenuVM : public NotifyPropertyChangedBase
{
public:
LeftPanelVM _left_panel_vm;
TopPanelVM _top_panel_vm;
...
};
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.
Code: Select all
<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>
Thanks for any help
-
sfernandez
Site Admin
- Posts: 2997
- Joined:
Re: Memory leak when binding commands
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?
Which objects are not correctly released in this scenario for you?
Re: Memory leak when binding commands
Hey, thanks for the quick response!
The issue was in the logic of closing the panel.
I replaced the TargetObject with TargetName and there are no more memory leaks
The issue was in the logic of closing the panel.
Code: Select all
<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>
Code: Select all
<i:EventTrigger EventName="Click">
<ei:ChangePropertyAction TargetName="FloatingWindow" PropertyName="IsOpen" Value="False"/>
</i:EventTrigger>
-
sfernandez
Site Admin
- Posts: 2997
- Joined:
Re: Memory leak when binding commands
Ok, that makes more sense to me. Great to know you solved it.
Who is online
Users browsing this forum: No registered users and 16 guests