5

I have a situation where my program crashes when I make a number of cursor.moveToNext() requests. The error message reads:

android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=773 (# cursors opened by this proc=773)
at android.database.CursorWindow.<init>(CursorWindow.java:112)
at android.database.CursorWindow.<init>(CursorWindow.java:100)
at android.database.AbstractWindowedCursor.clearOrCreateWindow(AbstractWindowedCursor.java:198)
at android.database.sqlite.SQLiteCursor.clearOrCreateWindow(SQLiteCursor.java:364)
at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:162)
at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:156)
at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:161)
at android.database.AbstractCursor.moveToNext(AbstractCursor.java:209)
at net.cunniman.teacherplannerlite.SchoolClassDataSource.getClassForDayView(SchoolClassDataSource.java:173)
at net.cunniman.teacherplannerlite.DayView.getSchoolClasses(DayView.java:200)
at net.cunniman.teacherplannerlite.DayView.displayDate(DayView.java:176)
at net.cunniman.teacherplannerlite.DayView.plusDay(DayView.java:287)
at net.cunniman.teacherplannerlite.DayView.onClick(DayView.java:93)
at android.view.View.performClick(View.java:3565)
at android.view.View$PerformClick.run(View.java:14165)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4514)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
at dalvik.system.NativeStart.main(Native Method)

try {
    this.openForRead();
    for (int day_counter = 0; day_counter < DAY_PERIOD.length/2; day_counter++)
    {
        Cursor cursor = database.rawQuery
            ("SELECT " + NAME + " FROM " + TABLE_NAME + " WHERE " + DAY_PERIOD[day_counter * 2]
            + " = '" + day + "' AND " + DAY_PERIOD[day_counter * 2 + 1] + " = "
            + Integer.toString(period), null);


    while (cursor.moveToNext())
    {
        String name = cursor.getString(0);
        sc.setName(name);
    }


} finally {
    this.close();
}

Does anyone know what might be causing this - seems like a memory leak (maybe?) but is there a way I can prevent it from happening?

Thanks

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Mark__C
  • 825
  • 1
  • 13
  • 24
  • you might not be closing all the cursor – nandeesh Aug 19 '12 at 19:53
  • 2
    You are not closing all cursors. After the while loop, add cursor.close. You are also create a lot of Cursor objects. I would suggest to review you code. It's better to do a "bigger" SQL, fetching all data, and then loop over that. – Tom Aug 19 '12 at 20:47
  • Thanks Tom, that's really helpful on both parts and makes sense. I'll try implementing tomorrow. – Mark__C Aug 20 '12 at 19:31
  • I've got a similar issue that happens on the emulator, but not on any devices I've tested it on. I am, however, closing all cursors. Any ideas? – Michell Bak Mar 20 '13 at 22:44

1 Answers1

8

Most often the cause for this error are non closed cursors. Make sure you close all cursors after using them (even in the case of an error).

Cursor cursor = null;
try {
    cursor = db.query(...
    // do some work with the cursor here. i.e.:
    while (cursor.moveToNext())
    { .... }
} finally {
    // this gets called even if there is an exception somewhere above
    if(cursor != null)
        cursor.close();
}
whlk
  • 15,487
  • 13
  • 66
  • 96
  • I've got a similar issue that happens on the emulator, but not on any devices I've tested it on. I am, however, closing all cursors. Any ideas? – Michell Bak Mar 20 '13 at 22:44
  • Sorry, can't help without any specific details. Better open a new question for this. – whlk Mar 20 '13 at 22:46
  • Yeah, had a feeling. It's a pretty complex issue in my case, so I'm just going to rewrite all the code first and see if that helps. – Michell Bak Mar 20 '13 at 22:52