DataContext not getting GC'd/Finalized
See original GitHub issueDiscussed in https://github.com/AvaloniaUI/Avalonia/discussions/12109
<div type='discussions-op-text'>Originally posted by nic547 July 9, 2023 Using the Avalonia .NET Core template, updating to Avalonia11 and editing the mainWindow like following:
public partial class MainWindow : Window
{
public MainWindow()
{
DataContext = new ViewModel();
InitializeComponent();
}
private void Button_OnClick(object? sender, RoutedEventArgs e)
{
new MainWindow().Show();
}
private void Button_GC_Click(object? sender, RoutedEventArgs e)
{
GC.Collect();
}
}
public class ViewModel
{
~ViewModel()
{
Debug.WriteLine("Finalized called");
}
private readonly byte[] someData = new byte[65536];
}
<Window xmlns="https://github.com/avaloniaui"
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"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="AvaloniaApplication1.MainWindow"
Title="AvaloniaApplication1">
<StackPanel>
<Button Click="Button_OnClick">Open New Window</Button>
<Button Click="Button_GC_Click">GC</Button>
</StackPanel>
</Window>
After opening and closing new windows, I cannot observe the “ViewModel” getting finalized. Additionally a Memory Snapshot (VS) will reveal as many Instances of the ViewModel as Windows were opened, despite only one being open.
Is there something I need to do to properly “clean up” a closed window?
(Full code: https://github.com/nic547/AvaloniaRepro)
Edit: Does work as expected in 11 preview 8, but not in 11 rc1.1 and 11
</div>Issue Analytics
- State:
- Created 2 months ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
View model doesn't get updated to DataContext
I think you do need understand that DataContext is a dependency property. Its definition looks like this:
Read more >WPF/MVVM: Child control recognize DataContext as Model ...
To fix this issue, you need to set the Command binding of the MenuItem to the MainWindowViewModel instead of the Project object. You...
Read more >FrameworkElement.DataContext Property (System.Windows)
Gets or sets the data context for an element when it participates in data binding. ... NeverLocalize)] public object DataContext { get; set;...
Read more >WPF - Binding - Cannot get C# properties register in XAML
WPF - Binding - Cannot get C# properties register in XAML. r/csharp - Binding shows that this property is not found in the...
Read more >DataContext is not notified on changes
My question is if there is a way to avoid the reassigment of the DataContext and it gets notified every time CurrentCustomer changed....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Fixed with #12430
@flexxxxer fyi in Fixed Issues, write Fixes #TheIssueNumber. “Fixes” and “Closes” are key words for Github.