title: Monotouch (Xamarin.iOS) memory leaks
body: <p>I have an app with a complicated view hierarchy, where one view controller (let's call it RootVC) can lead to many others. When I pop RootVC, I want to completely release it. I didn't store the reference in a field or elsewhere and believed that it would be enough. But recently I checked Xamarin's Heap Shot and saw in the "All objects" section that many instances of RootVC remain in memory. </p>
  
  <p>For example this:</p>
  
  <pre><code>sealed class VC1 : UIViewController
  {
      ...
  
      private void OpenVC2()
      {
          var vc2 = new VC2();
          NavigationController.PushViewController(vc2, true);
      }
  }
  
  sealed class VC2 : UIViewController
  {
      private UIButton button;
      public override ViewDidLoad()
      {
          ...
          button = new UIButton(frame);
          button.TouchUpInside += HandleButtonTouch;
          ...
      }
  
      private void HandleButtonTouch(object sender, EventArgs e)
      {
          NavigationController.PopViewControllerAnimated(true):
      }
  }
  </code></pre>
  
  <p>Here's the link to the screenshots of Heap Shot (can't post them as images because of low reputation) - <a href="http://imgur.com/a/3MYBr#1" rel="nofollow">http://imgur.com/a/3MYBr#1</a></p>
  
  <p>When I'm on VC2, I see something like on the first screenshot.</p>
  
  <p>When I pop VC2 and on VC1, Heap Shot look like on the second image.</p>
  
  <p>After a few VC2 appearing it looks like the third screenshot.</p>
  
  <p>And they never get disposed or finalized, they remain till the end. Even through they are not reachable via roots. Which is not acceptable because memory is limited and with all these VC2's in memory we can easily get memory warning or app terminating. </p>
  
  <p>But if I subscribe/unsubscribe to button events on View appearing/disappearing like this:</p>
  
  <pre><code>sealed class VC2 : UIViewController
  {
      private UIButton button;
      public override ViewDidLoad()
      {
          ...
          button = new UIButton(frame);
          ...
      }
  
      public override void ViewWillAppear(bool animated)
      {
          ...
          button.TouchUpInside += HandleButtonTouch;
      }
  
      public override void ViewDidDisappear(bool animated)
      {
          ...
          button.TouchUpInside -= HandleButtonTouch;
      }
  
      private void HandleButtonTouch(object sender, EventArgs e)
      {
          NavigationController.PopViewControllerAnimated(true):
      }
  }
  </code></pre>
  
  <p>In this case VC2 (with all its views, etc) gets disposed properly when it disappears. ~VC2() is called and all VC2's are no longer visible in Heap Shot.</p>
  
  <p>So, here's the question. What I am doing wrong? Should I subscribe/unsubscribe from control events like above? Or it's a Xamarin.iOS's memory leak?</p>