Silverlight Memory Leak via Event Handler
I am paraphrasing a quote from the Silverlight Forum as I am experience the same problem but am not 100% clear on how to fix this.
I have a large application that is experienceing memory leaks and it appears that the GC in Silverlight is not cleaning up after my event handlers, my fault I have learned. The memory leaks I've come across so far have been related to event handlers that have not been unhooked before the usercontrol is removed.
"Where I came across it was when I had a usercontrol in a datatemplate for a list. The usercontrol had an event handler being wired up in the constructor and I assumed it would be cleaned up when the items in the list are removed and the control is cleaned up. Not so, the event handler is a strong reference and the GC won't clean up the control as it still has a reference to it. "
I need to manually unhook the event in my code. I have searched my code and found many references to:
this.Loaded += method
What I'm not 100% clear on is where in my code should I put the
this.loaded -= method
to ensure that I have unhooked the event handler. This is most often occuring when opening an window or a button is being created.
I'm a bit new to Silverlight and not sure how to proceed.
Thank you in Advance!
CHS