Property Grid: Changing the SelectedObject causes memory leak

Open
#1,724 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
desktop

Research direction

Start with the supplied MainWindow/Button_Click reproduction and the XAML PropertyGrid SelectedObject binding. Repeat the 2,000 object changes, force collection as shown, and inspect whether PropertyItem-related objects remain retained; done means repeated changes no longer leave the reported growing instances.

Written by the indexing model from the issue text.

Description

Hello;
I need help; PropertyGrid 4.4.0 in .NET6 WPF
Every time I change the SelectedObject the ManagedMemeory increases. e.g. in the example below I change the SelectedItem 2000x and the objects will never get collected.

What am I doing wrong; Thx.

Managed Memeory after 2000x changes                                     Count   Size   Inclusive size      
Xceed.Wpf.Toolkit.PropertyGrid.PropertyItemCollection	                4000	224000	9570840
List<Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem>	                4000	240000	5577120
ObservableCollection<Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem>	4000	224000	6313120
Xceed.Wpf.Toolkit.PropertyGrid.ObjectContainerHelper	                4000	291312	4223768
Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem	                        2000	3764808	19228568
Xceed.Wpf.Toolkit.PropertyGrid.DescriptorPropertyDefinition	        2000	1952000	3007536

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MyModel SelectedItem { get; set; }
    public MyModel MyInstance1 { get; set; } = new MyModel();
    public MyModel MyInstance2 { get; set; } = new MyModel();
    public event PropertyChangedEventHandler? PropertyChanged;

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        for (int i = 0; i < 1000; i++)
        {
            SelectedItem = MyInstance1;
            SelectedItem = MyInstance2;
        }
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
    }
}

public class MyModel : INotifyPropertyChanged
{
    public string MyModelString { get; set; } = "testString";
    public event PropertyChangedEventHandler? PropertyChanged; 
}
 <Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <StackPanel>
            <Button Click="Button_Click" Content="ClickMe"/>
            <xctk:PropertyGrid SelectedObject="{Binding SelectedItem}"/>
        </StackPanel>
    </Grid>
</Window>
Dominant language
C#
Stars
4.2k
Forks
912
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from xceedsoftware/wpftoolkit

All issues in xceedsoftware/wpftoolkit

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.