0

I have detected a small memeory leak with leakCanary in my app that I can't fix.

The leak gets detected when I open my NavigationDrawer and opens a DialogView once and then close it and then open my navdrawer again a couple of times. EDIT: Opening the dialog and dismissing it once and just leaving the drawer open will also leak

See video demo here:

This is the stacktrace of the leak:

enter image description here

NavDrawerFragment.class: in the fragmetns onDetach() I set the references to null but it dosen't help either

@OnClick(R.id.navDrawer_newList_btn)
public void onNewListBtnClick() {
    addListDialog = new AddShoplistDialog();
    addListDialog.show(getFragmentManager(), AddShoplistDialog.FRAGMENT_TAG);

}


@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    selectionListener = (NavDrawerContract.OnSelectionListener) activity;
}

@Override
public void onDetach() {
    super.onDetach();

    //These dont't solve the leak
    selectionListener = null;
    addListDialog = null;
    recyclerAdapter = null;
}

NavDrawerListAdapter.class This is the RecyclerViewAdapter for the RecyclerView in the NavigationDrawer

public NavDrawerListAdapter(@NonNull Context context, @Nullable OrderedRealmCollection<Shoplist> data, NavDrawerContract.OnListItemActions selectionListener) {
    super(context, data, true);

    this.realm = Realm.getDefaultInstance();
    this.selectionListener = selectionListener;

}

//Called when NavDrawerFragment's onDestroyView() is called
public void onCloseRealm() {
    realm.close();
    selectionListener = null;
}

ViewHolder in the NavDrawerListAdapter.Class selectionListener is also used here in the adapters ViewHolder

public class Viewholder extends RecyclerView.ViewHolder implements PopupMenu.OnMenuItemClickListener, View.OnClickListener {


    public Viewholder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
        itemView.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
                      selectionListener.onSelectedList(getItem(getAdapterPosition()).getId(), true);
        selecetedPos = getAdapterPosition();
        notifyDataSetChanged();
    }

1 Answers1

0

I can only speculate, but I suppose you're leaking through NavDrawerListAdapter itself, maybe because you give Realm some listener that has a reference to your dialog / activity and that you do not clear.

Thomas Keller
  • 5,933
  • 6
  • 48
  • 80
  • realm is only used in the adapters constructor as: `new NavDrawerListInteractor(realm);` and in the adapters viewholder with `realm.executeTransaction(new Realm.Transaction()` My interactor and realm dont have any listener to it –  Mar 10 '17 at 16:20