I have a memory leak that I have not been able get rid of and its related to RadGridView or more specifically the binding of ItemsSource.
I have a RadGridView bound to a CollectionViewSource.View. Every time the user click "refresh" on the page the Source is looped through, each item is disposed of, then the list is cleared, then the list is repopulated. This works fine except the items that were in the Source are never destroyed through GC. Now, if I never bind the RadGridView to the collection then each time "refresh" is performed each item gets collected as expected but once its been bound nothing I do causes it to be collected until the browser is closed. I have even tried setting the binding to an empty binding, then rebinding it after the collection is populated - no luck.
I had this same problem in Silverlight 3 Microsofts DataGrid but it went away with Silverlight 4's DataGrid. Now that we switched to RadGridView I am having the problem again.
Here are some code samples, some of this is because I was trying different things to get past this issue.
private
void
SetupRowViewModels(IEnumerable<Deal> deals)
{
OnDealsLoading(
this
);
this
.IsScreenBusy =
true
;
Deals.Source =
null
;
DealList2.SuspendNotifications();
// call dispose on each item in list
DealList2.ForEach(d => d.Dispose());
// clear the list
DealList2.Clear();
DealList2.ResumeNotifications();
GC.Collect();
// items should be destroyed now, but if bound to radgridview it will not
//Deals = new CollectionViewSource();
Deals.Source = DealList2;
//if (Deals.Source.IsNull()) Deals.Source = DealList2;
//Deals.Source = DealList2;
//Deals.View.Filter = null;
DealList2.SuspendNotifications();
foreach
(Deal item
in
deals)
{
var rowViewModel =
new
DealRowViewModel(item,
this
);
this
.DealList2.Add(rowViewModel);
}
DealList2.ResumeNotifications();
if
(Deals.View.SortDescriptions.Count == 0)
{
Deals.View.SortDescriptions.Add(
new
SortDescription(
"RowEntity.LastUpdateTms"
, ListSortDirection.Descending));
}
//this.Deals.View.Filter = Deals_Filter;
Logging.TimerStop(
"ViewModel: Start to Finish"
);
this
.IsScreenBusy =
false
;
OnDealsLoaded(
this
);
}
void
ViewModel_DealsLoaded(
object
sender, EventArgs e)
{
tdg.Dispatcher.BeginInvoke(
delegate
{
BindingOperations.SetBinding(tdg, RadGridView.ItemsSourceProperty,
new
Binding(
"Deals.View"
));
});
}
void
ViewModel_DealsLoading(
object
sender, EventArgs e)
{
tdg.Dispatcher.BeginInvoke(
delegate
{
BindingOperations.SetBinding(tdg, RadGridView.ItemsSourceProperty,
new
Binding());
});
}
Another thing this brought up was if I run
BindingOperations.SetBinding(tdg, RadGridView.ItemsSourceProperty,
new
Binding());
I get 19 "A first chance exception of type 'System.ArgumentException' occurred in Telerik.Windows.Data" errors.
Then when
BindingOperations
.SetBinding(tdg, RadGridView.ItemsSourceProperty, new Binding("Deals.View"));
is ran I get
"
System.Windows.Data Error: BindingExpression path error: 'ContentHorizontalOffset' property not found on 'DealRowViewModel:Deal-Deal: 13654' 'Png.GcsAg.Modules.DealManagement.RowViewModels.DealRowViewModel' (HashCode=33105505). BindingExpression: Path='ContentHorizontalOffset' DataItem='DealRowViewModel:Deal-Deal: 13654' (HashCode=33105505); target element is 'System.Windows.Controls.Border' (Name='SelectionBackground'); target property is 'Clip' (type 'System.Windows.Media.Geometry')..
System.Windows.Data Error: BindingExpression path error: 'ContentHorizontalOffset' property not found on 'DealRowViewModel:Deal-Deal: 13654' 'Png.GcsAg.Modules.DealManagement.RowViewModels.DealRowViewModel' (HashCode=33105505). BindingExpression: Path='ContentHorizontalOffset' DataItem='DealRowViewModel:Deal-Deal: 13654' (HashCode=33105505); target element is 'System.Windows.Controls.Border' (Name='Background_Over'); target property is 'Clip' (type 'System.Windows.Media.Geometry')..
System.Windows.Data Error: BindingExpression path error: 'ContentHorizontalOffset' property not found on 'DealRowViewModel:Deal-Deal: 13654' 'Png.GcsAg.Modules.DealManagement.RowViewModels.DealRowViewModel' (HashCode=33105505). BindingExpression: Path='ContentHorizontalOffset' DataItem='DealRowViewModel:Deal-Deal: 13654' (HashCode=33105505); target element is 'System.Windows.Controls.Border' (Name='Background_Selected'); target property is 'Clip' (type 'System.Windows.Media.Geometry')..
System.Windows.Data Error: BindingExpression path error: 'ContentHorizontalOffset' property not found on 'DealRowViewModel:Deal-Deal: 13654' 'Png.GcsAg.Modules.DealManagement.RowViewModels.DealRowViewModel' (HashCode=33105505). BindingExpression: Path='ContentHorizontalOffset' DataItem='DealRowViewModel:Deal-Deal: 13654' (HashCode=33105505); target element is 'System.Windows.Controls.Border' (Name='Background_Invalid'); target property is 'Clip' (type 'System.Windows.Media.Geometry')..
System.Windows.Data Error: BindingExpression path error: 'ContentHorizontalOffset' property not found on 'DealRowViewModel:Deal-Deal: 13654' 'Png.GcsAg.Modules.DealManagement.RowViewModels.DealRowViewModel' (HashCode=33105505). BindingExpression: Path='ContentHorizontalOffset' DataItem='DealRowViewModel:Deal-Deal: 13654' (HashCode=33105505); target element is 'System.Windows.Controls.Border' (Name='PART_RowBorder'); target property is 'Clip' (type 'System.Windows.Media.Geometry')..
System.Windows.Data Error: BindingExpression path error: 'ContentHorizontalOffset' property not found on 'DealRowViewModel:Deal-Deal: 13654' 'Png.GcsAg.Modules.DealManagement.RowViewModels.DealRowViewModel' (HashCode=33105505). BindingExpression: Path='ContentHorizontalOffset' DataItem='DealRowViewModel:Deal-Deal: 13654' (HashCode=33105505); target element is 'System.Windows.Controls.ContentPresenter' (Name='PART_HierarchyChildPresenter'); target property is 'Clip' (type 'System.Windows.Media.Geometry')..
System.Windows.Data Error: BindingExpression path error: 'ContentHorizontalOffset' property not found on 'DealRowViewModel:Deal-Deal: 13654' 'Png.GcsAg.Modules.DealManagement.RowViewModels.DealRowViewModel' (HashCode=33105505). BindingExpression: Path='ContentHorizontalOffset' DataItem='DealRowViewModel:Deal-Deal: 13654' (HashCode=33105505); target element is 'System.Windows.Controls.Border' (Name=''); target property is 'Clip' (type 'System.Windows.Media.Geometry')..
" for each item in the collection
These binding errors only occur AFTER the initial binding, so the second time and on - not the first time.
Please help.