Memory leak on CollectionView.View.Refresh

Posted by Dabblernl on Stack Overflow See other posts from Stack Overflow or by Dabblernl
Published on 2010-03-12T10:27:44Z Indexed on 2010/03/18 6:11 UTC
Read the original article Hit count: 694

Filed under:
|
|
|

I have defined my binding thus:

<TreeView
        ItemsSource="{Binding UsersView.View}"
        ItemTemplate="{StaticResource MyDataTemplate}"
/>

The CollectionViewSource is defined thus:

private ObservableCollection<UserData> users;
public CollectionViewSource UsersView{get;set;}
UsersView=new CollectionViewSource{Source=users};
UsersView.SortDescriptions.Add(
          new SortDescription("IsLoggedOn",ListSortDirection.Descending);
UsersView.SortDescriptions.Add(
          new SortDescription("Username",ListSortDirection.Ascending);

So far, so good, this works as expected: The view shows first the users that are logged on in alphabetical order, then the ones that are not.

However, the IsLoggedIn property of the UserData is updated every few seconds by a backgroundworker thread and then the code calls:

UsersView.View.Refresh();

on the UI thread.

Again this works as expected: users that log on are moved from the bottom of the view to the top and vice versa. However: Every time I call the Refresh method on the view the application hoards 3,5MB of extra memory, which is only released after application shutdown (or after an OutOfMemoryException...)

I did some research and below is a list of fixes that did NOT work:

  • The UserData class implements INotifyPropertyChanged
  • Changing the underlying users collection does not make any difference at all: any IENumerable<UserData> as a source for the CollectionViewSource causes the problem.
    -Changing the ColletionViewSource to a List<UserData> (and refreshing the binding) or inheriting from ObservableCollection to get access to the underlying Items collection to sort that in place does not work.

I am out of ideas! Help?

EDIT: I found it: The Resource MyDataTemplate contains a Label that is bound to a UserData object to show one of its properties, the UserData objects being handed down by the TreeView's ItemsSource. The Label has a ContextMenu defined thus:

 <ContextMenu Background="Transparent" Width="325" Opacity=".8" HasDropShadow="True">

      <PrivateMessengerUI:MyUserData IsReadOnly="True" >

          <PrivateMessengerUI:MyUserData.DataContext>

              <Binding Path="."/>

          </PrivateMessengerUI:MyUserData.DataContext>

     </PrivateMessengerUI:MyUserData>

</ContextMenu>

The MyUserData object is a UserControl that shows All properties of the UserData object. In this way the user first only sees one piece of data of a user and on a right click sees all of it.

When I remove the MyUserData UserControl from the DataTemplate the memory leak disappears! How can I still implement the behaviour as specified above?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about wpf