There are two ways (that I know of) to cause an unintentional memory leak in C#:
- Not disposing of resources that implement
IDisposable - Referencing and de-referencing events incorrectly.
I don't really understand the second point. If the source object has a longer lifetime than the listener, and the listener doesn't need the events anymore when there are no other references to it, using normal .NET events causes a memory leak: the source object holds listener objects in memory that should be garbage collected.
Can you explain how events can cause memory leaks with code in C#, and how I can code to get around it using Weak References and without Weak References?