<p>Say we have the following method:</p>

<pre><code>private MyObject foo = new MyObject();

// and later in the class

public void PotentialMemoryLeaker(){
  int firedCount = 0;
  foo.AnEvent += (o,e) =&gt; { firedCount++;Console.Write(firedCount);};
  foo.MethodThatFiresAnEvent();
}
</code></pre>

<p>If the class with this method is instantiated and the <code>PotentialMemoryLeaker</code> method is called multiple times, do we leak memory?</p>

<p>Is there any way to unhook that lambda event handler after we're done calling <code>MethodThatFiresAnEvent</code>?  </p>

[{'answer_id': 16484, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 5, 'selected': True, 'text': "<p>Yes, save it to a variable and unhook it.</p>\n\n<pre><code>DelegateType evt = (o, e) =&gt; { firedCount++; Console.Write(firedCount); };\nfoo.AnEvent += evt;\nfoo.MethodThatFiresAnEvent();\nfoo.AnEvent -= evt;\n</code></pre>\n\n<p>And yes, if you don't, you'll <em>leak</em> memory, as you'll hook up a new delegate object each time. You'll also notice this because each time you call this method, it'll dump to the console an increasing number of lines (not just an increasing number, but for one call to MethodThatFiresAnEvent it'll dump any number of items, once for each hooked up anonymous method).</p>\n"}, {'answer_id': 16486, 'author': 'Nick Berardi', 'author_id': 17, 'author_profile': 'https://Stackoverflow.com/users/17', 'pm_score': 0, 'selected': False, 'text': '<p>Yes in the same way that normal event handlers can cause leaks.  Because the lambda is actually changed to:</p>\n\n<pre><code>someobject.SomeEvent += () =&gt; ...;\nsomeobject.SomeEvent += delegate () {\n    ...\n};\n\n// unhook\nAction del = () =&gt; ...;\nsomeobject.SomeEvent += del;\nsomeobject.SomeEvent -= del;\n</code></pre>\n\n<p>So basically it is just short hand for what we have been using in 2.0 all these years.</p>\n'}, {'answer_id': 16489, 'author': 'Wilka', 'author_id': 1367, 'author_profile': 'https://Stackoverflow.com/users/1367', 'pm_score': 2, 'selected': False, 'text': "<p>You wont just leak memory, you will also get your lambda called multiple times. Each call of 'PotentialMemoryLeaker' will add another copy of the lambda to the event list, and every copy will be called when 'AnEvent' is fired.</p>\n"}, {'answer_id': 16498, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 2, 'selected': False, 'text': "<p>Your example just compiles to a compiler-named private inner class (with field firedCount and a compiler-named method). Each call to PotentialMemoryLeaker creates a new instance of the closure class to which where foo keeps a reference by way of a delegate to the single method.</p>\n\n<p>If you don't reference the whole object that owns PotentialMemoryLeaker, then that will all be garbage collected. Otherwise, you can either set <em>foo</em> to null or empty foo's event handler list by writing this:</p>\n\n<pre><code>foreach (var handler in AnEvent.GetInvocationList()) AnEvent -= handler;\n</code></pre>\n\n<p>Of course, you'd need access to the <strong>MyObject</strong> class's private members. </p>\n"}, {'answer_id': 16516, 'author': 'Vaibhav', 'author_id': 380, 'author_profile': 'https://Stackoverflow.com/users/380', 'pm_score': 2, 'selected': False, 'text': '<p>Well you can extend what has been done <a href="http://diditwith.net/PermaLink,guid,aacdb8ae-7baa-4423-a953-c18c1c7940ab.aspx" rel="nofollow noreferrer">here</a> to make delegates safer to use (no memory leaks)</p>\n'}]
['https://Stackoverflow.com/questions/16473', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']