Memory leak checking - event handlers....

We are working with a large application, 8 developers, all using ReSharper 4.5 for C#.  AFter some research we have determined that we have a memory leak and it may be caused because of the two conditions below:

1. Event handlers that have not been unhooked before the usercontrol is removed.   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. The event needs to be unwired itself.  

Hooking up the event would be this.Loaded += method and unwiring it you use this.Loaded -= method;

I found this on the internet searching and I'm hoping that we can come up with a template or codestyle to help find this condition in our code?

2. When using DomainDataSource (DDS) and DataPager, all distinct entities of every loaded page are accumulated (and not released when moving to an other page) in the underlying EntityList.  I need to clear the EntityList but more important is can I search with Resharper to find the situations when in our code the developer did NOT clear their entity list?

Any Suggestions or Help would be most appreciated.

Cheers
CHS

Please sign in to leave a comment.