FragmentStatePagerAdapter is leaking and I can't figure out why when using LeakCanary

120 Views Asked by At

I'm using LeakCanary to help detect leaks and I keep getting this one leak, but I can't figure out why its happening.

I have a FragmentStatePagerAdapter thats using its default offscreen limit of loading one to each side.

All I have in my Fragment is loading an Image using Glide.

@Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_viewpager_image_view, container, false);
        ButterKnife.bind(this, view);


        Glide.with(mContext).load(mUrl)
                .apply(RequestOptions.centerInsideTransform())
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {

                        mProgressBar.setVisibility(View.INVISIBLE);
                        errorImageView.setVisibility(View.VISIBLE);
                        errorTextView.setVisibility(View.VISIBLE);

                        Glide.with(mContext)
                                .load(R.drawable.snail)
                                .apply(RequestOptions.centerInsideTransform())
                                .into(errorImageView);

                        String text = "Failed to load image \n" + mUrl;
                        errorTextView.setText(text);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        mProgressBar.setVisibility(View.INVISIBLE);
                        return false;
                    }
                })
                .into(mImage);

        return view;


    }

The LeakCanary message doesn't popup everytime I scroll so it's frustrating because I can't pinpoint how to recreate the error. I can scroll and load 30-50 images before the LeakCanary message popup.

├─ e.com.app.ui.viewpager.ViewpagerImageFragment$1 instance
│    Leaking: UNKNOWN
│    Anonymous class implementing com.bumptech.glide.request.RequestListener
│    ↓ ViewpagerImageFragment$1.this$0
│                               ~~~~~~
╰→ e.com.app.ui.viewpager.ViewpagerImageFragment instance
​     Leaking: YES (ObjectWatcher was watching this because e.com.app.ui.viewpager.ViewpagerImageFragment received Fragment#onDestroy() callback and Fragment#mFragmentManager is null)
​     key = 3e232914-78d0-457c-9ca5-c6e9c8f92c55
​     watchDurationMillis = 7300
​     retainedDurationMillis = 2300
​     key = f745901e-5222-42db-b594-f07a47b84788

What exactly is causing the leak?

1

There are 1 best solutions below

2
On BEST ANSWER

Use Glide.with(this), where this is the Fragment instance, with that your Glide request will be tied to your Fragment lifecycle rather than the Activity.