Analyzing events usage using a R# plugin

As you might already know, even if the .Net framework has a garbage collector, you can easily create memory leaks in your application.

The most common way to create a leak is to register to an event handler on a object that has a longer lifetime than the object where the handler is defined. The problem can also occurs by using static class such as EventManager (for more information see this blog post). Some .Net developers have been working on a way to go round the problem using Reflection, Weak Reference and other cool stuff. You can check out for example this excellent article on Code Project.

However, if you cannot change the way your declare events (because of internal policies in the company or because you don’t have the source code), you must be very carefull about the way you manage your events.

I’ve been working lately on a Resharper plugin that helps detecting events that are never unsubscribed. Basically, what is does is the following:

resharper_plugin

I’d like to have feedback from you .Net developpers, about whether you find such a plugin useful or not.

  • How do you deal with the event memory leak problem ?
  • Would you like to use my plugin ?
  • Would you like me to release it on a open source platform ?
  • What other kind of possible leaks are you thinking about to enhance the plugin ?

Please write a comment to let my know what you think. Thank you for your feedback !

4 thoughts on “Analyzing events usage using a R# plugin

  1. To answer your first question, I wrote a custom implementation of WeakEventManager that works on any event type, and pair it with a DelegatingWeakEvent class that implements IWeakEventListener and accepts a delegate as a constructor argument (which it invokes upon receiving the weak event).

    Being a fellow ReSharper Jedi, I think this is a great idea for a plugin. I generally like to see R# plugins released under some sort of open source license (permissive or reciprocal). That way if you lose interest or become slow to update it when the R# APIs change, we can recompile it ourselves :).

  2. Here’s an idea: provide a new annotation attribute along the lines of [UnloadMethod], which could indicate that a method is invoked to “unload” an object. If your plugin sees that an event is unsubscribed via a call chain involving the unload method, it can assume that the event will always be unsubscribed whenever that object is used. It should also understand the IDisposable pattern and assume that any event unsubscriptions stemming from Dispose() will always be performed.

  3. Mike,

    Do you have any pointer to your work about WeakEventManager ? Do you think it’s better than the implementation available in the CodeProject article ?

    And thank you for the feedback, I might contact you soon to discuss your ideas about the R# plugin :-)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>