When weak events are necessary
A reason for a lot of memory leaks in .Net applications is when event handlers are not deregistered. Basically, there are two kinds of components in an application. The first kind are components that live very long, sometimes as long as your application is running. These components have a "static" or "singleton" character (which doesn't mean that the class must be marked static). Beside these singleton instances, there are usually many instances of classes which do not live for such a long time - let's call them "dynamic" components. Now, what happens when there's a static component which defines an event, and a dynamic component registeres an event handler? This means that the static component references the dynamic component! This fact will prevent the garbage collector from destroying the dynamic component. So what we have to do is to (manually) deregister the event handler at the point when the dynamic component is not needed any more, which might not be easy to determine depending on the design of the system.Mehr...