Introduction
As you may know event handlers are a common source of memory leaks caused by the persistence of objects that are not used anymore, and you may think should have been collected, but are not, and for good reason.
In this (hopefully) short article, I’ll present the issue with event handlers in the context of the .Net framework, then I’ll show you how you can implement the standard solution to this issue, the weak event pattern, in two ways, either using:
- the “legacy” (well, before .Net 4.5, so not that old) approach which is quite cumbersome to implement
- the new approach provided by the .Net 4.5 framework which is as simple as it can be
(The source code is available here.)