c# - When subclassing Xamarin Android.Views.View (or any of its subclasses), do I need to call dispose on objects I created? -


if have class below create drawables , use them while button on page, standard dispose of of imagedrawable 's when overriding dispose method, or should dispose of them in ondetachedfromwindow, or not needed @ all.

1.

public class exampleimagebutton : imagebutton {     private ilist<animationdrawable> _animations;  ....      protected override void dispose (bool disposing)     {         if(disposing)         {             foreach(var item in _animations)              {                 item.dispose();             }             _animations = null;         }         base.dispose (disposing);     } } 

2.

public class exampleimagebutton : imagebutton {     private ilist<animationdrawable> _animations;  ....      protected override void ondetachedfromwindow()     {         foreach(var item in _animations)          {             item.dispose();         }         _animations = null;     } } 

it standard practice dispose() child objects within parents dispose() method. when object derives java.lang.object have corresponding java peer object.

after invoke dispose() on class subclasses java.lang.object, peer connection broken (as held intptr handle property in java.lang.object) , no longer safe use. dispose() marks object gc candidate in both mono , dalvik virtual machines.

in example above, safe way destroy ilist<animationdrawable> _animations in example 1. dispose guaranteed last method called before object has it's peer connection broken, shouldn't dispose child objects in other callbacks (such ondetachedfromwindow) unless absolutely certain won't used again.

further reading:


Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -