Bug 1257667 - Try to catch stale cursor usage using LeakCanary r?sebastian
MozReview-Commit-ID: Ga5lPgwpIxp
--- 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.