Bug 1257667 - Try to catch stale cursor usage using LeakCanary r?sebastian draft
authorAndrzej Hunt <andrzej@ahunt.org>
Thu, 17 Mar 2016 13:55:00 -0700
changeset 341857 b92305221cbcfac1e247ca0c4fd13ded9d6a575a
parent 341856 6ae8936372ab432a162aea846228325616f62241
child 516478 1bc25b24eba1033fbb68df2fc1a0a0121ed0d86f
push id13306
push userbmo:ahunt@mozilla.com
push dateThu, 17 Mar 2016 22:24:03 +0000
reviewerssebastian
bugs1257667
milestone47.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;
+
 abstract class SimpleCursorLoader extends AsyncTaskLoader<Cursor> {
     final ForceLoadContentObserver mObserver;
     Cursor mCursor;
 
     public SimpleCursorLoader(Context context) {
         super(context);
         mObserver = new ForceLoadContentObserver();
     }
@@ -67,16 +69,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.