This question is locked. New answers and comments are not allowed.
The garbage collector doesn't appear to be able to free my RadGridViews once I set their ItemsSource to an ObservableCollection.
Perhaps I am not really executing things correctly so I am hoping you can set me straight on this. I created a very simple project that demonstrates my problem. Unfortunately even a simple project is a lengthy post, so I appologize in advance for that.
btw: I am using the silverlight 3 controls, version 2009.1.529.1020
The project simply adds a UserControl that contains a RadGridView which binds to a very simple two property class. A button allows you to add one of these UserControls and another button removes it. Theoritically if you punch add, add, add, then remove, remove, remove, then garbage collect (also a button) you should be able to fire up WinDBG and notice that there are no instances of the class TestGarbage.AGridView in the heap. Unfortunately this is not what I see... HELP?
Here are the !DumpHeap -type TestGarbage.AGridView results from doing the previously mentioned steps:
| 0:026> .loadby sos coreclr |
| 0:026> !dumpheap -type TestGarbage.AGridView |
| ... |
| Address MT Size |
| 0482f6a8 06bd70a4 84 |
| 048e8cec 06bd70a4 84 |
| 049e6f60 06bd70a4 84 |
| total 3 objects |
| Statistics: |
| MT Count TotalSize Class Name |
| 06bd70a4 3 252 TestGarbage.AGridView |
And the !gcroot on the first item 0482f6a8
| 0:026> !gcroot 0482f6a8 |
| Note: Roots found on stacks may be false positives. Run "!help gcroot" for |
| more info. |
| Scan Thread 8 OSTHread f9c |
| Scan Thread 24 OSTHread 10d4 |
| Scan Thread 25 OSTHread 7ec |
| DOMAIN(02E9F5A8):HANDLE(Pinned):6be12f8:Root: 05814260(System.Object[])-> |
| 04820230(System.Collections.Generic.Dictionary`2[[System.IntPtr, mscorlib],[System.Object, mscorlib]])-> |
| 05818170(System.Collections.Generic.Dictionary`2+Entry[[System.IntPtr, mscorlib],[System.Object, mscorlib]][])-> |
| 048799b4(MS.Internal.ManagedObjectReference)-> |
| 0486cb40(Telerik.Windows.Data.FieldFilterDescription)-> |
| 0486cbe8(Telerik.Windows.Data.RadObservableCollection`1[[Telerik.Windows.Data.FilterDescriptor, Telerik.Windows.Data]])-> |
| 0486ccc4(System.Collections.Specialized.NotifyCollectionChangedEventHandler)-> |
| 0486ccac(System.Object[])-> |
| 0486cc8c(System.Collections.Specialized.NotifyCollectionChangedEventHandler)-> |
| 048656f4(Telerik.Windows.Controls.GridView.FilteringDropDown)-> |
| 048659a8(System.Windows.Controls.Grid)-> |
| 04865608(System.Windows.Controls.Grid)-> |
| 04865aa4(System.Windows.Controls.Grid)-> |
| 0486351c(Telerik.Windows.Controls.GridView.GridViewHeaderCell)-> |
| 04830a84(Telerik.Windows.Controls.RadGridView)-> |
| 0484574c(System.Windows.Controls.Grid)-> |
| 0482f6a8(TestGarbage.AGridView)-> |
The code follows ( I can also send a .RAR file that contains the project if that helps)
XAML for User Control that has the grid in it:
| <UserControl x:Class="TestGarbage.AGridView" |
| xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
| xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
| xmlns:telerikGrid="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView" |
| Loaded="UserControl_Loaded"> |
| <Grid x:Name="LayoutRoot" Background="White"> |
| <telerikGrid:RadGridView x:Name="cGridView" > |
| <telerikGrid:RadGridView.Columns> |
| <telerikGrid:GridViewDataColumn HeaderText="ID" DataMemberBinding="{Binding ID}" Width="Auto"/> |
| <telerikGrid:GridViewDataColumn HeaderText="Name" DataMemberBinding="{Binding Name}" Width="Auto" /> |
| </telerikGrid:RadGridView.Columns> |
| </telerikGrid:RadGridView> |
| </Grid> |
| </UserControl> |
.CS for same User Control
| using System; |
| using System.Collections.ObjectModel; |
| using System.Windows.Controls; |
| using System.Windows; |
| namespace TestGarbage |
| { |
| public partial class AGridView : UserControl |
| { |
| public AGridView() |
| { |
| InitializeComponent(); |
| } |
| private void UserControl_Loaded(object sender, RoutedEventArgs e) |
| { |
| var coll = new ObservableCollection<Simple>(); |
| coll.Add( new Simple { Name = "First", ID = "1" }); |
| coll.Add(new Simple { Name = "Second", ID = "2" }); |
| cGridView.ItemsSource = coll; |
| } |
| } |
| } |
.XAML for MAINPAGE in a new application
| <UserControl x:Class="TestGarbage.MainPage" |
| xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
| xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
| xmlns:telerikGrid="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView" |
| xmlns:local="clr-namespace:TestGarbage" |
| Width="400" Height="300"> |
| <StackPanel x:Name="LayoutRoot" Background="White" Orientation="Vertical"> |
| <Button Content="Add Control" Click="AddOne" /> |
| <Button Content="Remove OK" Click="RemoveOK" /> |
| <Button Content="Collect Garbage" Click="OnCollect" /> |
| <StackPanel x:Name="cStack" Orientation="Vertical" /> |
| </StackPanel> |
| </UserControl> |
.CS for MAINPAGE
| using System; |
| using System.Collections.Generic; |
| using System.Linq; |
| using System.Net; |
| using System.Windows; |
| using System.Windows.Controls; |
| using System.Windows.Documents; |
| using System.Windows.Input; |
| using System.Windows.Media; |
| using System.Windows.Media.Animation; |
| using System.Windows.Shapes; |
| using System.Collections.ObjectModel; |
| namespace TestGarbage |
| { |
| public partial class MainPage : UserControl |
| { |
| public MainPage() |
| { |
| InitializeComponent(); |
| } |
| private void |
| AddOne(object sender, RoutedEventArgs e) |
| { |
| var control = new AGridView(); |
| cStack.Children.Add( control ); |
| } |
| private void |
| OnCollect(object sender, RoutedEventArgs e) |
| { |
| GC.Collect(); |
| GC.WaitForPendingFinalizers(); |
| } |
| private void RemoveOK(object sender, RoutedEventArgs e) |
| { |
| cStack.Children.RemoveAt( cStack.Children.Count -1 ); |
| } |
| } |
| } |