Xamarin - preventing memory leaks in ViewHolder & Adapter pattern

We are managing references in our ViewHolder in a following way:

public class MusicListViewHolder : Java.Lang.Object, IImageLoadingListener, View.IOnClickListener
    {
        public static readonly float COVER_ART_THUMB_SIZE_PX = 90;

        public delegate void OptionSelectedDelegate(object item);
        public delegate void PlayPauseClickDelegate(object item);

        private TextView m_title;
        private TextView m_subtitle;
        private TextView m_infoRow;
        private ImageView m_art;
        private ImageView m_downloadState;
        private RelativeLayout m_optionRelativeLayout;
        private ImageView m_optionImageView;

The worry I am having is that this looks a little like as a memory leak as we do not dispose the referenced TextViews and ImageViews anywhere. As I learnt when working with Xamarin everything should be properly disposed.

Are these references somehow magically destroyed by the adapter? Does this actually leak? The only suitable method to override that I found was:

protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

Should I call m_title.Dispose()etc. in the override of protected override void Dispose(bool disposing)? Or is there anything else to this that I am missing?

Answers

  • mveroukismveroukis CAMember ✭✭✭

    No one had an answer for this?!? Only thing that comes to mind is that the adapter needs to keep a list of all viewholders and then when you dispose the recyclerview, you can dispose each of the references to widgets in the viewholders. Ugly to say the least, you'd think there would be a better way than that.

  • I'm doing it by keeping list of created viewholders and Disposing them on Adapter Dispose. Adapter is disposed when Recycler view is Disposed and that is disposed when page is Disposed :)

    But apparently, RecyclerView has some cache for every type, if you have many item types, and cache gets too big, it clears the cache. Would be nice, if we know about it and have chance to Dispose c# types also.

Sign In or Register to comment.