Bug 172 - UIViewController not being garbage collected when placed in a modal
Summary: UIViewController not being garbage collected when placed in a modal
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 4.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2011-08-04 03:00 UTC by Andrew Young
Modified: 2011-08-12 12:22 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

sample project (12.84 KB, application/zip)
2011-08-04 03:00 UTC, Andrew Young

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Andrew Young 2011-08-04 03:00:19 UTC
Created attachment 65 [details]
sample project

I have attached a sample app that shows that a UIViewController is not getting collected when being presented in a modal.

Steps to reproduce:
1. build and run project
2. touch the "Push a new controller" button
3. go back
4. verify that the console displays that the view controller was collected
5. now touch the "Show Modal" button
6. touch the "Cancel" button.
7. verify that no console message was written. nothing was finalized or GC'd.
Comment 1 Andrew Young 2011-08-04 20:25:57 UTC
If you create a private member reference to the MyNavController object and add this bit of code to the end of ViewDidAppear() then everything cleans up as it should. Is there any way around having to do this? This seems kind of hacky to prevent memory leaks.

if (_myNavController != null) {
   _myNavController.SetViewControllers (new UIViewController[0], false);
   _myNavController = null;
Comment 2 Jeffrey Stedfast 2011-08-10 13:20:51 UTC
Just worked through this with Miguel and it is definitely a bug in MonoTouch and we have tracked it down, now we just gotta go through the history and find out why a ref was being retained for this.
Comment 3 Andrew Young 2011-08-10 13:25:57 UTC
Thanks for the feedback. Another thing I've noticed, which might be a separate issue, is that a UIViewController that is popped off of a UINavigationController via the default back button (you know, the one that's shaped like an arrow" does not get cleaned up either. But if you manually pop the controller off the stack with the following code snippet, it cleans up like it should.

var backButton = new UIBarButtonItem(UIBarButtonSystemItem.Cancel, (sender, args) => 
NavigationItem.SetLeftBarButtonItem(backButton, false);
Comment 4 Jeffrey Stedfast 2011-08-12 12:19:19 UTC
Okay, this is fixed in git master now. We've already branched for monotouch-4.1, but I suspect since we found a regression in that branch, we'll be re-branching which means this fix should land in what will be released as 4.1

For those interested, the problem was that the superview was caching references to its children, not allowing them to be disposed properly.

The fix was to make RemoveFromSuperview() remove the object from its cache as well.

This bug actually lead us down the path to discovering that we had a lot of other similar issues with objects being cached causing them not to be disposed of as expected, which have also hopefully been fixed now.
Comment 5 Andrew Young 2011-08-12 12:22:08 UTC
Jeffrey, I think I have found a similar issue in MKMapView where annotations added to the map view are not released unless I manually call RemoveAnnotation(annotation).

Should I start a new bug for this?