title: WPF & Prism 4.1 Garbage Collection / Memory Issues
body: <p>I built a Prism application using WPF, .Net 4, Prism 4.1, and Unity. I'm using a DirectoryModuleCatalog to find modules at runtime. My views are displayed in a TabControl (MainRegion). When I remove a view from the region, the view and viewmodel remain in memory and never get garbage collected - the tabitem is removed. After many hours searching, I cannot figure out what I'm doing wrong.</p>
  
  <p>Here's my bootstrapper:</p>
  
  <pre><code>public class Bootstrapper : UnityBootstrapper
  {
      protected override void InitializeShell()
      {
          base.InitializeShell();
          App.Current.MainWindow = (Window)Shell;
          App.Current.MainWindow.Show();
      }
  
      protected override DependencyObject CreateShell()
      {
          var shell = new Shell();
          return shell;
      }
  
      protected override IModuleCatalog CreateModuleCatalog()
      {
          return new DirectoryModuleCatalog() { ModulePath = @".\Modules" };
      }
  }
  </code></pre>
  
  <p>Here's my module:</p>
  
  <pre><code>[Module(ModuleName = "ModuleA")]
  public class Module : IModule
  {
      private IRegionManager _regionManager;
  
      public Module(IRegionManager regionManager)
      {
          _regionManager = regionManager;
      }
  
      public void Initialize()
      {
          var view = new UserControl1();
          //_regionManager.RegisterViewWithRegion("MainRegion", typeof(UserControl1));
          _regionManager.Regions["MainRegion"].Add(view, "ModuleA");
          _regionManager.Regions["MainRegion"].Activate(view);
      }
  }
  </code></pre>
  
  <p>And heres the viewmodel for my view that gets added to the region:</p>
  
  <pre><code>public class ViewModel
  {
      public DelegateCommand RemoveView { get; set; }
  
      public ViewModel()
      {
          RemoveView = new DelegateCommand(() =&gt;
              {
                  var regionManager = ServiceLocator.Current.GetInstance&lt;IRegionManager&gt;();
                  var view = regionManager.Regions["MainRegion"].GetView("ModuleA");
                  regionManager.Regions["MainRegion"].Deactivate(view);
                  regionManager.Regions["MainRegion"].Remove(view);
              });
      }
  }
  </code></pre>
  
  <p>And here's the code behind for the view:</p>
  
  <pre><code>public partial class UserControl1 : UserControl
  {
      public UserControl1()
      {
          InitializeComponent();
  
          this.DataContext = new ViewModel();
      }
  }
  </code></pre>
  
  <p>I've read that it could be because I'm instantiating the view in the module or perhaps the viewmodel in the view? When I use Red Gate Memory Profiler, and remove the view via the DelegateCommand, the view and viewmodel are both flagged as not being able to be garbage collected. Where is the reference that I'm not properly cutting?</p>
  
  <p>Heres the retention graph from Ants: <a href="https://docs.google.com/file/d/0B4XjO9pUQxBXbGFHS1luNUtyOTg/edit?usp=sharing" rel="nofollow">https://docs.google.com/file/d/0B4XjO9pUQxBXbGFHS1luNUtyOTg/edit?usp=sharing</a></p>
  
  <p>Here's a <a href="https://docs.google.com/file/d/0B4XjO9pUQxBXVEtJaW8yYWV1SGs/edit?usp=sharing" rel="nofollow">test solution</a> showing the issue.</p>
  
  <p>Also, I posted the question on <a href="http://compositewpf.codeplex.com/discussions/434331" rel="nofollow">CodePlex</a> as well.</p>