Trinea / android-auto-scroll-view-pager

Android auto scroll viewpager or viewpager in viewpager
http://p.codekk.com/
Apache License 2.0
1.67k stars 670 forks source link

AutoScrollViewPager.mContext leaks DemoActivity instance (48 KB) #33

Open Logan676 opened 7 years ago

Logan676 commented 7 years ago

logs

net.tsz.afinal.core.AsyncTask.sHandler * references net.tsz.afinal.core.AsyncTask$InternalHandler.mQueue * references android.os.MessageQueue.mMessages * references android.os.Message.next * references android.os.Message.target * references cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager$MyHandler.this$0 * references cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager.mContext * leaks cn.com.demo.DemoActivity instance * Retaining: 48 KB. * Reference Key: 2cd1903-be4e-4d3e-8f94-efa50c058ec7 * Device: Xiaomi Xiaomi MI NOTE Pro leo * Android Version: 5.1.1 API: 22 LeakCanary: 1.5 00f37f5 * Durations: watch=15287ms, gc=231ms, heap dump=3338ms, analysis=54623ms 
gavin0x00 commented 7 years ago

I meet the same problem,this is a serious bug!

Logan676 commented 7 years ago

the patch below should fix the leak


+    @Override
+    protected void onDetachedFromWindow() {
+        super.onDetachedFromWindow();
+        if (handler != null) {
+            handler.removeCallbacksAndMessages(null);
+        }
+    }
+
rajatgupta1993 commented 7 years ago

@Logan676 where to write this piece of code? I am using this library in a fragment , onDetachedFromWindow() is not available in Fragment. Which function to override for my scenario

Logan676 commented 7 years ago

maybe you can still place the patch in activity, what`s the matter is how to make fragment talk with activity, there are several ways to do that.