This is a migrated thread and some comments may be shown as answers.

How to prevent memory leak

5 Answers 217 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Tyree
Top achievements
Rank 2
Tyree asked on 15 Sep 2010, 02:48 PM

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.

5 Answers, 1 is accepted

Sort by
0
Stefan Dobrev
Telerik team
answered on 16 Sep 2010, 02:25 PM
Hi Tyree,

I have created a small sample application that ties to mimics your scenario based on your sample code. I have run it locally and it is not leaking on my side. Can you please take a look at the code and provide the necessary changes to illustrate the leak you are experiencing?

Hope this helps,
Stefan Dobrev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Arun
Top achievements
Rank 1
answered on 21 Jan 2011, 09:07 AM
Hi

I am also facing the similar problem with the RadGridView. I have posted in the forum and no reply.

The example you have provided itself is showing the increase in memory[you can see the iexplorer.exe is showing increase in memory in Task manager] when i keep on setting the item source and clear it and again set the item source and clear it again

i tried the GC collect and no use. Please verify it and let me know what could be the issue

Arun
0
Vlad
Telerik team
answered on 21 Jan 2011, 09:42 AM
Hello,

 Please check your other thread for more info:
http://www.telerik.com/community/forums/silverlight/gridview/memory-leak-issue-with-radgridview.aspx

Greetings,
Vlad
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Geoff Hardy
Top achievements
Rank 1
answered on 02 Jan 2013, 11:52 PM
Was this ever resolved? The link that Vlad posted no longer appears to be valid.
0
Vlad
Telerik team
answered on 03 Jan 2013, 07:54 AM
Hi,

 This was related to Silverlight 4 and most of the leaks were resolved directly in the framework. 

All the best,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
GridView
Asked by
Tyree
Top achievements
Rank 2
Answers by
Stefan Dobrev
Telerik team
Arun
Top achievements
Rank 1
Vlad
Telerik team
Geoff Hardy
Top achievements
Rank 1
Share this question
or