c# - Will a UITableViewController Garbage Collect if it instances a nested class referencing itself in a variable? -


i follow pattern of creating uitableview's in monotouch (xamarin.ios) setting .source property new instance of nested uitableviewsource class shown below. concern brought attention developer uitableview class never garbage collected because of nested class's reference parent class through _parentcontroller property assigned in constructor of nested class. belief long nested class holding reference parent class unable collected.

can confirm if true , creating uitableview's in manner not programming practice garbage collection unable free resources? (some methods , constructors left out conciseness.

public partial class myviewcontroller : uitableviewcontroller {    public override viewdidload()    {       base.viewdidload();       this.tableview.source = new viewsource(this);    }     public class viewsource : uitableviewsource    {       myviewcontroller _parentcontroller;       public viewsource(myviewcontroller parentcontroller)       {          _parentcontroller = parentcontroller;       }    } } 

it released.

the reason "source" property weak property in objective-c, means assignment there not call retain on object, merely keeps reference it.

strong cycles prevent gc happening happen if objective-c calls retain on passed object.

the following example shows how data released, have first drill down bunch of times, drill up.

public class myviewcontroller : uitableviewcontroller {     public override void viewdidload()     {         base.viewdidload();         this.tableview.source = new viewsource(this);     }      public class viewsource : uitableviewsource     {         public override int rowsinsection (uitableview tableview, int section)         {             return 100;         }          public override uitableviewcell getcell (uitableview tableview, nsindexpath indexpath)         {             return new uitableviewcell (uitableviewcellstyle.default, "foo");         }          public override void rowselected (uitableview tableview, nsindexpath indexpath)         {             var n = appdelegate.window.rootviewcontroller uinavigationcontroller;             n.pushviewcontroller (new myviewcontroller (), true);             gc.collect ();         }         myviewcontroller _parentcontroller;         public viewsource(myviewcontroller parentcontroller)         {             _parentcontroller = parentcontroller;         }     }     ~myviewcontroller ()     {         console.writeline ("disposing");     } }  [register ("appdelegate")] public partial class appdelegate : uiapplicationdelegate {     public static uiwindow window;     public override bool finishedlaunching (uiapplication app, nsdictionary options)     {         window = new uiwindow (uiscreen.mainscreen.bounds);          var myviewcontroller = new myviewcontroller ();         window.rootviewcontroller = new uinavigationcontroller (myviewcontroller);         window.makekeyandvisible ();         return true;     } } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -