question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DataContext not getting GC'd/Finalized

See original GitHub issue

Discussed 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:closed
  • Created 2 months ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
flexxxxercommented, Aug 6, 2023

Fixed with #12430

0reactions
timuniecommented, Aug 7, 2023

@flexxxxer fyi in Fixed Issues, write Fixes #TheIssueNumber. “Fixes” and “Closes” are key words for Github.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found