Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
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
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.
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.
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 . The fix is quite easy, just call Dispose in your DismissDelegate.
 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).