Closed
Description
Glide Version:4.6.1
Integration libraries:
Okhttpversion:4.6.1
Device/Android Version:
Issue details / Repro steps / Use case background:
Glide load line / GlideModule
(if any) / list Adapter code (if any):
private void loadImage(ViewHolder holder, int position) {
WeakHandler mHandler = new WeakHandler();
Runnable runnable = new Runnable() {
@Override
public void run() {
loadImage(holder, position);
}
};
Glide.with(context).
load(getUri(position))
.apply(getFitCenter())
.listener(new Listener() {
@Override
public void onRetry() {
mHandler.postDelayed(runnable, 2500);
handlerRunnableMapCategory.put(mHandler, runnable);
}
})
.into(imagViewProduct);
WeakHandler is Memory safer implementation of android.os.Handler
Here handlerRunnableMapCategory
is a map where I am storing the instance of handler and runnable, and In onDestroy
I am clearing the map and removeCallbacksAndMessages
, And Its is working, but still I am getting Leak through LeakCanary
Activity
hussainahmad commentedon May 3, 2018
sjudd commentedon May 7, 2018
handlerRunnableMapCategory
in the first example is leaking memory regardless of the weak handler. None of the weak handler or runnables you're using above should be necessary.Glide will keep a reference to the Target as long as the request is in progress. You can cancel them manually via
Glide.with(Context/Fragment/Activity)).clear(Target)
. If you use the appropriate lifecycle (Activity/Fragment) Glide will clear pending requests when the lifecycle goes throughonDestroy
.hussainahmad commentedon May 8, 2018
But without
handler
it will crash the app with the reasonjava.lang.IllegalStateException: You can't start or clear loads in RequestListener or Target callbacks. If you must do so, consider posting your into() or clear() calls to the main thread using a Handler instead
use Handler#post to post your into() , and I debuggedhandlerRunnableMapCategory
is clearing and and setting null inonDestroy
, Is there any best approach to retry image inonLoadFailed
?stale commentedon May 15, 2018
This issue has been automatically marked as stale because it has not had activity in the last seven days. It will be closed if no further activity occurs within the next seven days. Thank you for your contributions.
sjudd commentedon May 15, 2018
Ah sorry I missed that onRetry is called in Glide's RequestListener callback. In that case you do need to post, though without a delay.
In what I can see of the leak stack traces, it looks like the Glide request hasn't been cleared. If the
Context
you're using is not the activity context, you'll need to clear the request manually withGlide.with(context).clear(target)
.hussainahmad commentedon May 16, 2018
@sjudd I am using
ApplicationContext
, I think that's the issue. SoGlide
is unable to clear, I have to do this manually.