title: WPF Window not being destroyed if Content s from another source
body: <p>Firstly, I am very proficient at WPF and rarely get thrown by things, and rarely ever have to post a question. To be fair, I have only ever used one main Window with all embedded Content so it is really flat so this is the first time I have had to use additional floating Windows.</p>
  <p>I have a Window that I raise after assigning it a View Model to its Content properly which duly shows and renders as expected using the data template etc. I hook into the Window.Close event after creation and remove the Close even method when closed. Pseudocode as follows:</p>
  <p>Creating the Window:</p>
  <pre><code>private void CreateWin()
  {
    // Just in case a previous instance is open
    CloseWin();
  
    _myWin = new Window()
    {
      Content = new MyViewModel()
    };
  
    _myWin.Closed += OnMyWinClosed;
    _myWin.Show();
  }
  </code></pre>
  <p>When the Window is closed:</p>
  <pre><code>private void OnMyWinClosed(object sender, EventArgs e)
  {
    CloseWin();
  }
  </code></pre>
  <p>Cleanind up:</p>
  <pre><code>private void CloseWin()
  {
    if (_myWin == null)
      return;
  
    _myWin.Content = null;
    _myWin.Closed -= OnMyWinClosed;
    _myWin = null;
  }
  </code></pre>
  <p>I run the code to create the Window and set the content to the view model and when the I close the Window it soon gets destroyed and all is OK.
  Now the issue:</p>
  <p>The view model that I am assigning is not necessarily complex but it does a few things, even then, it is probably not relevant but, if I assign an existing view model that is still in memory rather than a new instance of the same view model type (as this code shows), I can create 50 instances of the Window but they do not get destroyed until the application terminates.</p>
  <p>My actual window is a subclassed Window so that I can add a breakpoint to the ~ destructor to check the finalisation. I have also called the following just to make sure but no destruction, although a bit over the top:</p>
  <pre><code>GC.Collect()
  GC.WaitForPendingFinalizers();
  GC.Collect()
  </code></pre>
  <p>You will also notice, for extra measure, that I set the Window.Content = null afterward just to really detach it from the Window.</p>
  <p>Nothing in the associated view, model, or data template has any events hooking the view model to the UI other than the inbuilt PropertyChanged. However, that should also make no difference, and also the same view model independently set has no issue.</p>
  <p>My application went from about 110MB to 420MB when opening all so I could also see the memory creep up. I have a good laptop so is it that it would eventually clear but takes longer for larger objects? If that is the case then that does not make sense.</p>
  <p>In summary, it almost seems that ViewModel’s assigned to a Window.Content property will prevent the Window from being garbage collected if the ViewModel is from another source and not created on the fly for the specific Window.</p>
  <p>Surely that cannot be right?</p>
  <p>Other points:
  I have tried setting the Owner and not setting the Owner which makes no difference.
  I have created these windows in a loop of 100 and destroying them to see if has anything to do with bigger objects taking a long time, but no change.</p>