WPF DataGrid memory leak in VS2022
See original GitHub issueThis issue has been moved from a ticket on Developer Community.
[severity:I’m unable to use this version] [regression] [worked-in:16.11.3] Original Source I have this code, and no matter what I do I keep getting a memory leak from the DataGrid data update. I’ve been looking at all the other answers to this problem here for days and I haven’t found anything that works for me. I have a WPF window and this code to update the data (it is a small version of what happens in the real code, but I get the same bug). Works fine in VS2019.
I have this WPF code:
<Window x:Class="TremendoMemoryLeak.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
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"
xmlns:local="clr-namespace:TremendoMemoryLeak"
mc:Ignorable="d"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DataGrid ItemsSource="{Binding Hmis, Mode=OneWay}" AutoGenerateColumns="False" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Estado">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Ellipse Fill="{Binding Estado}" HorizontalAlignment="Left" Height="25" Stroke="Black" VerticalAlignment="Top" Width="25"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Dato1" Binding="{Binding Dato1}"/>
<DataGridTextColumn Header="Dato2" Binding="{Binding Dato2}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
This class code:
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public ObservableCollection<Datos> Hmis { get; } = new ObservableCollection<Datos>();
private System.Threading.Timer timer;
public MainWindow()
{
InitializeComponent();
timer = new System.Threading.Timer(ActualizacionUI_Tick, null, 1000, 1000);
}
public event PropertyChangedEventHandler? PropertyChanged;
private void ActualizacionUI_Tick(object data)
{
Datos dato1 = new Datos() { Dato1 = "1", Dato2 = "2" };
Datos dato2 = new Datos() { Dato1 = "1", Dato2 = "2" };
Datos dato3 = new Datos() { Dato1 = "1", Dato2 = "2" };
Dispatcher.Invoke(() =>
{
dato1.Estado = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF5DC75D"));
dato2.Estado = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF5DC75D"));
dato3.Estado = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF5DC75D"));
Hmis.Clear();
Hmis.Add(dato1);
Hmis.Add(dato2);
Hmis.Add(dato3);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Hmis"));
});
}
}
And this data class:
public class Datos : INotifyPropertyChanged
{
public Brush Estado { get; set; }
public string Dato1 { get; set; }
public string Dato2 { get; set; }
public event PropertyChangedEventHandler? PropertyChanged;
}
Original Comments
Feedback Bot on 10/20/2021, 11:07 PM:
We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.
Original Solutions
(no solutions)
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:8 (4 by maintainers)
Top GitHub Comments
Probed in Framework versions 4.6.1/4.8 and net 5: Example SLN: TremendoMemoryLeak.zip Only happens in visual studio 2022
@singhashish-wpf I don’t know how to correctly implement a DependencyProperty version.