Note that there are some explanatory texts on larger screens.

plurals
  1. POUIViewController not garbage collected when started modal
    text
    copied!<p>I’m having a memory issue in my iPhone app. I'm using <a href="http://en.wikipedia.org/wiki/Mono_%28software%29#MonoTouch" rel="nofollow">MonoTouch</a>. I have hunted down the problem, by using a static instance counters. The problem has something to do with modal view controllers. When I navigate from a root-viewcontroller to a first-level-viewcontroller and back, I find that the first-level-viewcontroller is garbage-collected. But when I make the first-level-viewcontroller modal by calling <code>PresentModalViewController</code>, and I return by calling <code>DismissModalViewControllerAnimated</code>, I find that the first-level-viewcontroller is not garbage collected. Not even when I call <code>GC.Collect()</code>.</p> <p>Why not? Am I doing something wrong?</p> <p>What is the best practice for ensuring release of view controllers?</p> <pre><code>partial class RootViewController : UITableViewController { static int instanceCount; static int nextId; int instanceId; public RootViewController (IntPtr handle) : base(handle) { instanceCount++; instanceId = nextId++; Console.WriteLine(string.Format("RootViewController #{0} Count={1}", instanceId, instanceCount)); } public override void ViewDidLoad () { base.ViewDidLoad (); Title = "Root"; NavigationItem.RightBarButtonItem = new UIBarButtonItem("ModalVC", UIBarButtonItemStyle.Plain, delegate { var firstlevelVc = new FirstLevelViewController(); PresentModalViewController(new UINavigationController(firstlevelVc), true); }); NavigationItem.LeftBarButtonItem = new UIBarButtonItem("PushVC", UIBarButtonItemStyle.Plain, delegate { var firstlevelVc = new FirstLevelViewController(); NavigationController.PushViewController(firstlevelVc, true); }); } public override void ViewDidAppear (bool animated) { base.ViewDidAppear (animated); GC.Collect(); } ~RootViewController() { instanceCount--; Console.WriteLine(string.Format("RootViewController #{0} Count={1}", instanceId, instanceCount)); } } public partial class FirstLevelViewController : UIViewController { static int instanceCount; static int nextId; int instanceId; public FirstLevelViewController (IntPtr handle) : base(handle) { Initialize (); } [Export("initWithCoder:")] public FirstLevelViewController (NSCoder coder) : base(coder) { Initialize (); } public FirstLevelViewController () : base("FirstLevelViewController", null) { Initialize (); } void Initialize () { instanceCount++; instanceId = nextId++; Console.WriteLine(string.Format("FirstLevelViewController #{0} Count={1}", instanceId, instanceCount)); } public override void ViewDidLoad () { base.ViewDidLoad (); Title = "1. level"; NavigationItem.RightBarButtonItem = new UIBarButtonItem("Dismiss modal", UIBarButtonItemStyle.Plain, delegate { ParentViewController.DismissModalViewControllerAnimated(true); }); } ~FirstLevelViewController() { instanceCount--; Console.WriteLine(string.Format("FirstLevelViewController #{0} Count={1}", instanceId, instanceCount)); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload