[ACCEPTED]-WeakReference and event handling-weak-references
It is good to get in the habit of unsubscribing 6 from events when you can, but sometimes 5 there isn't an obvious "cleanup" method 4 where it can be done. We recently posted 3 a blog article on this subject; it includes methods 2 that make it easy to subscribe to an event 1 with a WeakReference.
Weak delegate pattern is something that 10 should be there in CLR. Normal events exhibit 9 "notify me while you are alive" semantics, while 8 often we need "notify me while I'm 7 alive". Just having delegate on WeakReference 6 is wrong, because delegate is an object 5 too and even when recepient is still alive 4 and have incoming references, delegate itself 3 is only being referenced by said WeakReference 2 and will be collected instantly. See this old post for 1 an example of implementation.
Weak references in their own right, don't 11 solve the problem as the delegate holds 10 the reference. In the Composite Application 9 Library which ships with Prism (www.microsoft.com/compositewpf) there 8 is a WeakDelegate class that you could pull 7 from the source. The WeakDelegate basically 6 ues reflection and creates a delegate only 5 for a moment in time and then releases it, thereby 4 no holding any pointers. Within CAL it is 3 used by the EventAggregator class, but you 2 are free to rip it out for your own usage 1 as it is under MS-PL.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.