title: In WPF: Children.Remove or Children.Clear doesn't free objects
body: <p><em>Update</em>: I tried this on another, more cleanly installed, machine. I could not reproduce this on that machine. If I find out what offending (VSStudio) component causes this, I will let you know.</p>
  
  <p>I create some UIElements from code behind and was anticipating the garbage collection to clear up stuff. However, the objects are not free-ed at the time I expected it. I was expecting them to be freeed at RemoveAt(0), but they are only freed at the end of the program.</p>
  
  <p>How can I make the objects be freed when removed from the Children collection of the Canvas?</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Window1" Height="300" Width="300"
      MouseDown="Window_MouseDown"&gt;
    &lt;Grid&gt;
      &lt;Canvas x:Name="main" /&gt;
    &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>The code behind is:</p>
  
  <pre><code>public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
    }
  
  private void Window_MouseDown(object sender, MouseButtonEventArgs e)
  {
    GC.Collect(); // This should pick up the control removed at a previous MouseDown
    GC.WaitForPendingFinalizers(); // Doesn't help either
  
    if (main.Children.Count == 0)
      main.Children.Add(new MyControl() { Background = Brushes.Yellow, Width = 100, Height = 50 });
    else
      main.Children.RemoveAt(0);
   }
  }
  
  public class MyControl : UserControl
  {
    ~MyControl()
    {
      Debug.WriteLine("Goodbye");
    }
  }
  </code></pre>