ethanhua / Skeleton

A library provides an easy way to show skeleton loading view like Facebook and Alipay
3.67k stars 454 forks source link

Leak canary detects memory leaks when using view based skeleton #35

Open apexkid opened 5 years ago

apexkid commented 5 years ago
skeletonScreen = Skeleton.bind(rootView)
                    .color(R.color.tlShimmerColor)
                    .load(R.layout.item_skeleton_view_profile)
                    .show();

Leads to FrameLayout being leaked. My analysis is that this is due to this function:

private ShimmerLayout generateShimmerContainerLayout(ViewGroup parentView) {
        final ShimmerLayout shimmerLayout = (ShimmerLayout) LayoutInflater.from(mActualView.getContext()).inflate(R.layout.layout_shimmer, parentView, false);
        shimmerLayout.setShimmerColor(mShimmerColor);
        shimmerLayout.setShimmerAngle(mShimmerAngle);
        shimmerLayout.setShimmerAnimationDuration(mShimmerDuration);
        View innerView = LayoutInflater.from(mActualView.getContext()).inflate(mSkeletonResID, shimmerLayout, false);
        ViewGroup.LayoutParams lp = innerView.getLayoutParams();
        if (lp != null) {
            shimmerLayout.setLayoutParams(lp);
        }
        shimmerLayout.addView(innerView);
        shimmerLayout.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
            @Override
            public void onViewAttachedToWindow(View v) {
                shimmerLayout.startShimmerAnimation();
            }

            @Override
            public void onViewDetachedFromWindow(View v) {
                shimmerLayout.stopShimmerAnimation();
            }
        });
        shimmerLayout.startShimmerAnimation();
        return shimmerLayout;
    }

A new object is being inflated which internally uses the rootView but it is never destroyed explicitly.

bhpcode commented 4 years ago

setting mViewReplacer to null fixed this issue (did it using reflection as a quick hack).
don't think anyone is managing this project or responding to PRs now.

avinashbanswada commented 4 years ago

where is mViewReplacer, I don't see in the above code

avinashbanswada commented 4 years ago

setting mViewReplacer to null fixed this issue (did it using reflection as a quick hack). don't think anyone is managing this project or responding to PRs now.

Hi @bhpcode Can you please eloborate how you fixed this, I am facing same issue