Is Xamarin UITableView Source weak or strong?

754 views Asked by At

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.

1

There are 1 answers

0
Stephane Delcroix On

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 their obj-C counterparts.

In your (2nd) case, the newly created ViewSource will be kept alive as long as the TableView lives. I'm speaking about managed objects here. At the time the TableView is no longer used, Xamarin.iOS GC will flag it, as well as the ViewSource, 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.