Bug 1257667 - Try to catch stale cursor usage using LeakCanary r=sebastian
authorAndrzej Hunt <andrzej@ahunt.org>
Thu, 17 Mar 2016 13:55:00 -0700
changeset 289540 787e4d0b72b84a59086bf3ebe7cbedbd5d6773db
parent 289539 9ecc776c8824f4a0e17e67dbba935c83f2db3dfd
child 289541 6db8a54fb215eaf8821e829eb1885a2311817fcc
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [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 The ordering in this commit is now correct, whereas the previously pushed commit hadn't addressed the review comment. 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).
  *
@@ -83,16 +85,20 @@ abstract class SimpleCursorLoader extend
         mCursor = cursor;
 
         if (isStarted()) {
             super.deliverResult(cursor);
         }
 
         if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) {
             oldCursor.close();
+
+            // 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);
         }
     }
 
     /**
      * 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.
      *