ICollectionView

49 views
Skip to first unread message

CBl...@eki-consulting.com

unread,
Jan 4, 2011, 2:03:45 PM1/4/11
to wpf-di...@googlegroups.com
As some of you may know, I really like using CollectionViews in my
ViewModels. I even got Laurent to use PagedCollectionView in the WCF RIA
Services section of his new book.

Recently however, I became aware of the memory leak inherent in their
design. Every ICollectionView implementation I know of subscribes to the
OnCollectionChanged event of the source collection with no way to force the
ICollectionView to unsubscribe. In my current ViewModels, this is not a
problem as my PagedCollectionViews live as long as their source collections
anyway so this hasn't been a problem. However, in an application where the
source collections have a longer lifetime than the ViewModels that create
the CollectionViews I could forsee this becoming a massive memory leak
problem.

I am wondering what the correct solution would be. Should CollectionViews
all implement IDisposable as well?

Peter O'Hanlon

unread,
Jan 4, 2011, 2:43:09 PM1/4/11
to wpf-di...@googlegroups.com
I prefer the idea of WeakCollectionViews. Weak references are a much better idea to my mind.
--
Peter O'Hanlon

Sacha Barber

unread,
Jan 5, 2011, 4:09:44 AM1/5/11
to wpf-di...@googlegroups.com
I say use weak event everywhere you can, there are so many nasty gotchas out there. We had one the other day, when someone has a an attached behaviour where they were binding to a ICommand.CanExecuteChanged in DP. 

Bad memory leak right there, and it was used a lot.

WeakEvent proxy to the rescue
--
Sacha Barber
sacha....@gmail.com
Reply all
Reply to author
Forward
0 new messages