title: C# Manage memory leaks with events
body: <p>We have a nice tool by redgate called <a href="https://www.red-gate.com/products/dotnet-development/ants-memory-profiler/" rel="nofollow">Memory Profiler</a>. With it, we can easily check for memory leaks.</p>
  
  <p>Now, in C# .Net 4.0, we're trying to figure the best way to handle events so not to have memory leaks but we cannot have any!! Everything we tried leave no trace! Here's what we tried so far:</p>
  
  <hr>
  
  <ol>
  <li><ul>
  <li>In a limited scope, create a subscriber and publisher. Have subscriber subscribe to publisher's MyEvent</li>
  <li>On dispose, the publisher sets its MyEvent to null</li>
  <li>Exit scope</li>
  <li>Result: no memory leak.</li>
  </ul></li>
  </ol>
  
  <hr>
  
  <ol start="2">
  <li><ul>
  <li>In a limited scope, create a subscriber and publisher. Have subscriber subscribe to publisher's MyEvent</li>
  <li>On dispose, the publisher loops through MyEvent invocation list and remove each of then from MyEvent</li>
  <li>Exit scope</li>
  <li>Result: no memory leak.</li>
  </ul></li>
  </ol>
  
  <hr>
  
  <ol start="3">
  <li><ul>
  <li>In a limited scope, create a subscriber and publisher. Have subscriber subscribe to publisher's MyEvent</li>
  <li>Exit scope</li>
  <li>Result: no memory leak.</li>
  </ul></li>
  </ol>
  
  <hr>
  
  <p>Now, there are various answers on SO on how we should manage events handlers, but none of them bring actual proof that there's any leak to be had. With our tool, we aim to have an actual result for an actual problem, but we can't have the problem in the first place.</p>
  
  <p>Any idea how to ACTUALLY provoke a memory leak with events? Examples for .Net 4.5 or 4.6 will also do fine.</p>