Bug 1257667 - Try to catch stale cursor usage using LeakCanary r=sebastian
☠☠ backed out by 9ecc776c8824 ☠ ☠
authorAndrzej Hunt <andrzej@ahunt.org>
Thu, 17 Mar 2016 13:55:00 -0700
changeset 313253 ca8d1e1caeefdcd45bd608adec7a10c3632ceab5
parent 313252 95c8cb6f15151ddcc575efccabf8de28ad6b7673
child 313254 9ecc776c8824f4a0e17e67dbba935c83f2db3dfd
push id9480
push userjlund@mozilla.com
push dateMon, 25 Apr 2016 17:12:58 +0000
treeherdermozilla-aurora@0d6a91c76a9e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssebastian
bugs1257667
milestone48.0a1
Bug 1257667 - Try to catch stale cursor usage using LeakCanary r=sebastian MozReview-Commit-ID: Ga5lPgwpIxp
mobile/android/base/java/org/mozilla/gecko/home/SimpleCursorLoader.java
--- a/mobile/android/base/java/org/mozilla/gecko/home/SimpleCursorLoader.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/SimpleCursorLoader.java
@@ -18,16 +18,18 @@
  */
 
 package org.mozilla.gecko.home;
 
 import android.content.Context;
 import android.database.Cursor;
 import android.support.v4.content.AsyncTaskLoader;
 
+import org.mozilla.gecko.GeckoApplication;
+
 /**
  * A copy of the framework's {@link android.content.CursorLoader} that
  * instead allows the caller to load the Cursor themselves via the abstract
  * {@link #loadCursor()} method, rather than calling out to a ContentProvider via
  * class methods.
  *
  * For new code, prefer {@link android.content.CursorLoader} (see @deprecated).
  *
@@ -82,16 +84,20 @@ abstract class SimpleCursorLoader extend
         Cursor oldCursor = mCursor;
         mCursor = cursor;
 
         if (isStarted()) {
             super.deliverResult(cursor);
         }
 
         if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) {
+            // Trying to read from the closed cursor will cause crashes, hence we should make
+            // sure that no adapters/LoaderCallbacks are holding onto this cursor.
+            GeckoApplication.getRefWatcher(getContext()).watch(oldCursor);
+
             oldCursor.close();
         }
     }
 
     /**
      * Starts an asynchronous load of the list data. When the result is ready the callbacks
      * will be called on the UI thread. If a previous load has been completed and is still valid
      * the result may be passed to the callbacks immediately.