Note that there are some explanatory texts on larger screens.

plurals
  1. POIs this a bug in MonoTouch GC?
    text
    copied!<blockquote> <p>Note: <a href="http://dl.dropbox.com/u/76095045/MTMemoryTest.zip" rel="nofollow noreferrer">I've created a simple project</a>—you can see how switching types between <code>UIButton</code> and <code>CustomButton</code> in storyboard changes GC behavior.</p> </blockquote> <p>I'm trying to get my head wrapped around MonoTouch garbage collector.<br> The issue is similar to <a href="https://stackoverflow.com/q/4902967/458193">the one fixed in MT 4.0</a>, however with inherited types.</p> <p>To illustrate it, consider two view controllers, parent and child. </p> <p>Child's view contains a single <code>UIButton</code> that writes to console on tap.<br> Controller's <code>Dispose</code> method throws an exception so it's hard to miss.</p> <p>Here goes child view controller:</p> <pre><code>public override void ViewDidLoad () { base.ViewDidLoad (); sayHiButton.TouchUpInside += (sender, e) =&gt; SayHi(); } } void SayHi() { Console.WriteLine("Hi"); } protected override void Dispose (bool disposing) { throw new Exception("Hey! I've just been collected."); base.Dispose (disposing); } </code></pre> <p>Parent view controller just presents child controller and sets a timer to dismiss it and run GC:</p> <pre><code>public override void ViewDidLoad () { base.ViewDidLoad (); var child = (ChildViewController)Storyboard.InstantiateViewController("ChildViewController"); NSTimer.CreateScheduledTimer(2, () =&gt; { DismissViewController(false, null); GC.Collect(); }); PresentViewController(child, false, null); } </code></pre> <p>If you run this code, it predictably crashes inside <code>ChildViewController.Dispose()</code> called from its finalizer because child controller has been garbage collected. Cool.</p> <p>Now open the storyboard and change button type to <code>CustomButton</code>. MonoDevelop will generate a simple <code>UIButton</code> subclass:</p> <pre><code>[Register ("CustomButton")] public partial class CustomButton : UIButton { public CoolButton (IntPtr handle) : base (handle) { } void ReleaseDesignerOutlets() { } } </code></pre> <p>Somehow changing the button type to <code>CustomButton</code> is enough to trick garbage collector into thinking child controller is not yet eligible for collection.</p> <p>How is that so?</p>
 

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