Skip to content
This repository has been archived by the owner on Feb 26, 2018. It is now read-only.

[fix] leaking the last request object after request queue is drained #64

Merged
merged 1 commit into from Jun 26, 2015

Conversation

shaobin0604
Copy link

description:

this leak can be detected by LeakCanary, see

D/LeakCanary﹕ In io.github.pandacast:1.1:2.
D/LeakCanary﹕ * io.github.pandacast.MainActivity has leaked:
D/LeakCanary﹕ * GC ROOT thread com.android.volley.NetworkDispatcher. (named 'Thread-5690')
D/LeakCanary﹕ * references io.github.pandacast.model.RestClient$LiveListRequest.mListener
D/LeakCanary﹕ * references io.github.pandacast.LiveListFragment$LiveListAdapter.mContext
D/LeakCanary﹕ * leaks io.github.pandacast.MainActivity instance

root cause:

In NetworkDispatcher.run() , the local variable request hold
the last Request object. If there is no more Request object in
mQueue, request = mQueue.take(); in line 90 would block
until new Request object enqueue, which cause the leak of last
Request object.

In common practice, Request has strong reference to Listener and
Listener is either implemented by Activity or referenced by
Activity as instance of inner class which implement Listener.
Thus cause leaking the whole Activity instance.

how to fix:

set request to null before request = mQueue.take();

description:
  this leak can be detected by LeakCanary, see

  D/LeakCanary﹕ In io.github.pandacast:1.1:2.
  D/LeakCanary﹕ * io.github.pandacast.MainActivity has leaked:
  D/LeakCanary﹕ * GC ROOT thread
  com.android.volley.NetworkDispatcher.<Java Local> (named 'Thread-5690')
  D/LeakCanary﹕ * references
  io.github.pandacast.model.RestClient$LiveListRequest.mListener
  D/LeakCanary﹕ * references
  io.github.pandacast.LiveListFragment$LiveListAdapter.mContext
  D/LeakCanary﹕ * leaks io.github.pandacast.MainActivity instance

root cause:
  In NetworkDispatcher.run(), the local variable `request` hold
  the last Request object. If there is no more Request object in
  `mQueue`, `request = mQueue.take();` in line 90 would **block**
  until new Request object enqueue, which cause the leak of last
  Request object.

  In common pracice, Request has strong reference to Listener and
  Listener is either implmented by Activity or referenced by
  Activity as instance of inner class which implement Listener.
  Thus cause leaking the whole Activity instance.

how to fix:
  set request to **null** before `request = mQueue.take();`
@qluan
Copy link

qluan commented Jun 25, 2015

lgtm

mcxiaoke added a commit that referenced this pull request Jun 26, 2015
[fix] leaking the last request object after request queue is drained
@mcxiaoke mcxiaoke merged commit 8d06be9 into mcxiaoke:master Jun 26, 2015
@fantasyRqg
Copy link

great!
I have the same problem, and it take me long time to find out whats going on.
I use weak reference everywhere ... it is a hard and foolish work ...

@mcxiaoke
Copy link
Owner

released at v1.0.17

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants