2013-08-31 32 views
1

Xamarin是否會導致C#內存泄漏?即可以釋放MyViewController還是具有循環引用,防止它?Xamarin UITableView Source是弱還是強?

在MyViewController:

this.TableView.Source = new ViewSource(this); 


public class ViewSource : UITableViewSource 
{ 
    private readonly MyViewController parentController; 

    public ViewSource(MyViewController parentController) 
    { 
     this.parentController=parentController; 
    } 
} 

據:

Will a UITableViewController Garbage Collect if it instances a nested class referencing itself in a variable?這是沒有問題的。但是如果Source是弱點,那麼會發生什麼,如果你只有:

this.TableView.Source = new ViewSource(); 

Source可以從下一行發佈?無論哪種方式,這似乎是一個非常危險的API。

回答

1

別人可能會說比我好,但用幾句話說:別擔心你的第二種情況。 GC問題主要發生在Xamarin.iOS上,如果您的管理對象中的週期數在obj-C對應項中有很強的關係。

在您的(第2)情況下,只要TableView居住,新創建的ViewSource將保持有效。我在這裏談論託管對象。當TableView不再使用時,Xamarin.iOS GC將標記它,以及ViewSource,它們都將被丟棄。

這不幸是Xamarin.iOS的少數漏洞抽象之一,你需要一些底層Obj-C的知識來作出設計決定。

相關問題