1

I've been getting a few crashes due to this exception, but can't replicate it on any of my own devices/emulator.

The stacktrace from google play console

  android.database.CursorWindowAllocationException: 
  at android.database.CursorWindow.<init> (CursorWindow.java:108)
  at android.database.AbstractWindowedCursor.clearOrCreateWindow (AbstractWindowedCursor.java:198)
  at android.database.sqlite.SQLiteCursor.fillWindow (SQLiteCursor.java:138)
  at android.database.sqlite.SQLiteCursor.getCount (SQLiteCursor.java:132)
  at android.database.AbstractCursor.moveToPosition (AbstractCursor.java:220)
  at android.database.AbstractCursor.moveToNext (AbstractCursor.java:269)
  at rpuls.yatzysheets.DBController.getGameTime (DBController.kt:264)
  at rpuls.yatzysheets.PreviousGames$onCreateView$1.invoke (PreviousGames.kt:78)
  at rpuls.yatzysheets.PreviousGames$onCreateView$1.invoke (PreviousGames.kt:23)
  at org.jetbrains.anko.support.v4.SupportKt.UI (Support.kt:62)
  at rpuls.yatzysheets.PreviousGames.onCreateView (PreviousGames.kt:33)
  at android.support.v4.app.Fragment.performCreateView (Fragment.java:2337)
  at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1419)
  at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState (FragmentManager.java:1740)
  at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1809)
  at android.support.v4.app.BackStackRecord.executeOps (BackStackRecord.java:799)
  at android.support.v4.app.FragmentManagerImpl.executeOps (FragmentManager.java:2580)
  at android.support.v4.app.FragmentManagerImpl.executeOpsTogether (FragmentManager.java:2367)
  at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute (FragmentManager.java:2322)
  at android.support.v4.app.FragmentManagerImpl.execPendingActions (FragmentManager.java:2229)
  at android.support.v4.app.FragmentManagerImpl$1.run (FragmentManager.java:700)
  at android.os.Handler.handleCallback (Handler.java:790)
  at android.os.Handler.dispatchMessage (Handler.java:99)
  at android.os.Looper.loop (Looper.java:164)
  at android.app.ActivityThread.main (ActivityThread.java:6651)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:438)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:810)

The function that is throwing it

fun getGameTime(gameId: Long): String{
    val db = this.readableDatabase
    val cursor = db.rawQuery("SELECT ${gameTable.timestamp} FROM ${gameTable.table} WHERE ${gameTable.id} = ${gameId}", null)
    var result = ""
    if(cursor.moveToNext()){
        result = cursor.getString(cursor.getColumnIndex(gameTable.timestamp))
    }
    db.close()
    return result
}

As I google CursorWindowAllocationException almost every result is something related to not closing the cursor, and out of memory causing the moveToNext() to throw the exception.

How ever, in the stacktrace that I got there is nothing about out of memory and I believe that my cursor would be closed since the readableDatabase database that it lived on is closed?

The devices where the error occured

Basically a modern phone with 2GB ram. It happened once in the past 60 days, but I have seen it few months back.

The app is approximately run on 40 devices each day, so that is 1 crash in 2400 runs - not much, but I would like to know why this is happening.

Themelis
  • 4,048
  • 2
  • 21
  • 45
Rasmus Puls
  • 3,009
  • 7
  • 21
  • 58
  • Can you elaborate a little? Since I cant reproduce the crash, it is very difficult to test if such a change will have a positive impact. How should it prevent this error ? – Rasmus Puls Feb 25 '19 at 12:13
  • 1
    Possible duplicate of [android.database.CursorWindowAllocationException when moving a Cursor](https://stackoverflow.com/questions/21219039/android-database-cursorwindowallocationexception-when-moving-a-cursor) – Martin Zeitler Feb 25 '19 at 16:53
  • Duplicate of that one? Where do you see exceeding allocated memory or count of open cursors in my stack trace? – Rasmus Puls Feb 25 '19 at 18:43
  • all I can see is an un-handled exception; how about using using `try/catch`? that another query not having closed it's cursor appears likely... so how you want to define what the problem is, while not even knowing what the problem is? – Martin Zeitler Feb 25 '19 at 22:06
  • Try/catch could be a decent way to prevent a crash until the real cause is found. Still don't think the answer in your link is any help here, or answer any of my questions: will closing readableDatabase not close it's children cursor? Also this does not seem to have anything to do with memory exceeded. So why would you assume it is the same problem while not knowing what the problem is? – Rasmus Puls Feb 26 '19 at 14:45

1 Answers1

1

I would say that your work is cursor is a bit weird. And I'm not sure that the cursor will be closed upon database close. I would suggest doing anyway:

if (cursor.moveToFirst()) {
    result = cursor.getString(cursor.getColumnIndex(gameTable.timestamp))
}
cursor.close()
db.close()
Demigod
  • 5,073
  • 3
  • 31
  • 49