Bug 5003 - When presenting/dismissing a view controller the view and controller are not being released
Summary: When presenting/dismissing a view controller the view and controller are not ...
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 5.2
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2012-05-10 11:34 UTC by Philip Richardson
Modified: 2012-05-10 17:20 UTC (History)
2 users (show)

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

XCode and MonoTouch view controller tests (45.94 KB, application/zip)
2012-05-10 11:34 UTC, Philip Richardson

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 Philip Richardson 2012-05-10 11:34:17 UTC
Created attachment 1849 [details]
XCode and MonoTouch view controller tests

There seems to be an inconsistancy between the behavior of UIViewController between X Code and MonoTouch.

In X Code, if a modal view controller is presented using presentViewController and dismissed via dismissViewControllerAnimated both the view and controller are released from memory. On the other hand, under MonoTouch, DismissViewController does not release the view and the controller.

I've found a workaround that seems to force the the code to cleanup correctly, but am unsure of side effects.

Attached are two projects, one, for the X Code behavior, the other has a test example in MonoTouch with the workaround in an ifdef block.
Comment 1 Rolf Bjarne Kvinge [MSFT] 2012-05-10 17:20:31 UTC
This is an unfortunate side-effect of having to deal with two types of memory management at the same time (refcounting in ObjC and garbage collection in MonoTouch). In short there is a circular reference crossing managed and native objects which the garbage collector is unable to break [1]. The fix is quite easy, just call Dispose in your DismissDelegate.

[1] The TestViewController as a list of subviews, and in there there's a button (which the TestViewController has a native reference to). The button has a managed reference to the TestViewController in the TouchUpInside event handler. The garbage collector must ensure the button is not freed as long as there are any native references to it (since it cannot determine who has that reference), and the TestViewController will stay alive as long as the button is alive due to the managed reference in the event handler. When you call Dispose you're clearing the list of subviews, thus breaking the native reference between the TestViewController and the button. The button will then be freed, and then the TestViewController can be freed too.

Note that ~TestViewController will not be called when Dispose is called (manually).