Why and How to avoid Event Handler memory leaks ?

Posted by gillyb on Stack Overflow See other posts from Stack Overflow or by gillyb
Published on 2010-12-24T14:30:14Z Indexed on 2010/12/24 14:54 UTC
Read the original article Hit count: 168

Hey There,

I just came to realize, by reading some questions and answers on StackOverflow, that adding event handlers using += in C# (or i guess, other .net languages) can cause common memory leaks...

I have used event handlers like this in the past many times, and never realized that they can cause, or have caused, memory leaks in my applications.

How does this work (meaning, why does this actually cause a memory leak) ?
How can I fix this problem ? Is using -= to the same event handler enough ?
Are there common design patterns or best practices for handling situations like this ?
Example : How am I supposed to handle an application that has many different threads, using many different event handlers to raise several events on the UI ?

Are there any good and simple ways to monitor this efficiently in an already built big application ?

Thanks in advance!

© Stack Overflow or respective owner

Related posts about c#

Related posts about design-patterns