Can this cause a memory leak in C#
Xamarin or not? i.e. can MyViewController be released or does it have a circular reference, preventing it?
In MyViewController:
this.TableView.Source = new ViewSource(this);
public class ViewSource : UITableViewSource
{
private readonly MyViewController parentController;
public ViewSource(MyViewController parentController)
{
this.parentController=parentController;
}
}
According to:
Will a UITableViewController Garbage Collect if it instances a nested class referencing itself in a variable? it is not a problem. But if Source is a weak, then what happens if you just have:
this.TableView.Source = new ViewSource();
Source can be released from the next line? Either way this seems a very dangerous API.
Someone else will probably said it better than me, but in a few words: Don't worry about your second case.
GC
issues happens mainly on Xamarin.iOS if you have cycles in managed object that have a strong relation in theirobj-C
counterparts.In your (2nd) case, the newly created
ViewSource
will be kept alive as long as theTableView
lives. I'm speaking about managed objects here. At the time theTableView
is no longer used, Xamarin.iOSGC
will flag it, as well as theViewSource
, and they both will be disposed.That's unfortunately one of the few Leaky Abstraction of Xamarin.iOS, where you need some knowledge of the underlying Obj-C to make a design decision.