title: Will an anonymous-delegate event listener prevent garbage collection?
body: <p>I'm unsure whether a child window is able to be garbage-collected in the following scenario.  </p>
  
  <ol>
  <li>User control contains a "show popup" command</li>
  <li>The command creates a child window, and adds an anonymous listener for the "Closed" event.</li>
  </ol>
  
  <hr/>
  
  <pre><code>public partial class MainPage : UserControl
  {
      public ICommand PopupCommand { get; private set; }
  
      public MainPage()
      {
          InitializeComponent();
  
          PopupCommand = new DelegateCommand(arg =&gt; 
          {
              var child = new ChildWindow();
              child.Closed += (sender, args) =&gt;
              {
                  MessageBox.Show("You closed the window!");
              };
              child.Show();
          });
      }
  }
  </code></pre>
  
  <p>Since <code>PopupCommand</code>'s delegate still ostensibly contains a reference the the local <code>child</code> variable, will each invocation of <code>PopupCommand</code> leak memory?  Or will the garbage collector somehow recognize that it can dispose of <code>child</code> after it has been closed?</p>
  
  <hr/>
  
  <p><strong>Related: <a href="https://stackoverflow.com/q/9591195/1001985">detaching anonymous listeners from events in C# and garbage collection</a></strong></p>