Reputation:
I have an attached property which I use on a bunch of controls throughout my application. This property subscribes to a static event and has an event handler where some properties of the control the attached property is bound to are modified.
The problem is that some of these controls get recreated (for example, they are part of an ItemsControl or a Window which can be closed and re-opened) and then kept in memory through the attached property. In my WinForms version I subscribed to the disposed event for the control to know when it was removed, and then unsubscribed the event handlers. However, WPF does not have an event like this. Unloaded which get called when it is temporarily removed (like through a tab control changing tab).
So how can I remove these control references when the controls should be removed to avoid memory leaks? Currently if I refresh one of my pages the memory my app uses keeps increasing.
Upvotes: 0
Views: 335
Reputation:
I found a solution which seems to work. Instead of saving a reference to the control, I set it in a WeakReference object to allow the Garbage Collector to collect it.
Upvotes: 1