1

Can someone explain to me the comment here:

Don't create anonymous class of Target when calling Picasso as might get garbage collected. Keep a member field as a strong reference to prevent it from being gc'ed

Per line 30 of ImageViewAction.java, that Callback is a strong reference.

ImageViewAction(Picasso picasso, ImageView imageView, Request data, boolean skipCache,
      boolean noFade, int errorResId, Drawable errorDrawable, String key, Callback callback) {
    super(picasso, imageView, data, skipCache, noFade, errorResId, errorDrawable, key);
    this.callback = callback;
  }

Assuming the Callback is an anonymous class, it would create a reference to its parent class, thereby preventing the parent from being GC'd too.

Per line 48 of Action.java, the target itself is a WeakReference, but that is not the callback.

  Action(Picasso picasso, T target, Request data, boolean skipCache, boolean noFade,
      int errorResId, Drawable errorDrawable, String key) {
    this.picasso = picasso;
    this.data = data;
    this.target = new RequestWeakReference<T>(this, target, picasso.referenceQueue);

Can someone explain what I am misunderstanding?

esilver
  • 27,713
  • 23
  • 122
  • 168
  • 1
    Someone must hold a reference to your `Target` otherwise it will get gc'ed because Picasso only stores a `WeakReference` to it. – dnkoutso Dec 10 '13 at 21:28
  • Ah, I see - your comment only applied to the Target and not the Callback. It should be OK to use anonymous Callbacks then, yes? – esilver Dec 10 '13 at 21:33
  • Yes anonymous callbacks are kept as strong reference. It is HIGHLY recommended you invoke `cancel(target)` to free up the reference and prevent a temporary leak until Picasso download finishes. – dnkoutso Dec 11 '13 at 16:39

1 Answers1

0

I was confused and the comment referred to the Target (ImageView) not the Callback. The pattern of using an anonymous Callback is fine.

esilver
  • 27,713
  • 23
  • 122
  • 168