ios - Not calling dealloc on UIViewController in Xamarin -


i have uiviewcontroller written in native library bound xamarin app. controller has method follows:

+ (instancetype)newinstance {     return [shareddata.sharedinstance.mainstoryboard instantiateviewcontrollerwithidentifier:@"worksheetcontroller"]; } 

this method bound xamarin app follows:

// @interface worksheettableviewcontroller : uiviewcontroller [basetype(typeof(uiviewcontroller))] interface worksheettableviewcontroller {     // +(instancetype)newinstance;     [static]     [export("newinstance")]     worksheettableviewcontroller newinstance(); } 

now in xamarin app in uiviewcontroller subclass in viewdidload call method follows:

this.worksheetcontroller = worksheettableviewcontroller.newinstance(); 

the worksheetcontroller not used anywhere else in uiviewcontroller. when uiviewcontroller disposed (by popping off navigation stack), dealloc method of worksheettableviewcontroller not called.

however if change code in viewdidload follows:

this.worksheetcontroller = (worksheettableviewcontroller)shareddata.sharedinstance.mainstoryboard.instantiateviewcontroller("worksheetcontroller"); 

when uiviewcontroller disposed, dealloc method gets called.

to me there doesn't seem difference in newinstance method in native code , directly calling instantiateviewcontroller in xamarin. yet in former case dealloc not being called, while fine in later.

can explain why memory management different in 2 cases?

this because arc implicitly retain return value native newinstance method [1], , xamarin.ios not implicitly detect that.

you have 2 options:

  1. rename native method doesn't start new or copy (there few other prefixes well, see [1] full list), arc doesn't automatically retain return value.

  2. add [return: release ()] api definition (this tells xamarin.ios arc doing, , xamarin.ios add additional release call):

    [static] [export("newinstance")] [return: release ()] worksheettableviewcontroller newinstance(); 

[1] http://clang.llvm.org/docs/automaticreferencecounting.html#method-families


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

how to prompt save As Box in Excel Interlop c# MVC 4 -

xslt 1.0 - How to access or retrieve mets content of an item from another item? -