hari86
hari86

Reputation: 669

Android :CursorWindowAllocationException- even when closing the cursor

Iam getting the above mentioned exception even after the closing the cursor.

Below is my code

public Cursor fetchAllFreeGradeNr(String grade) {
        open();
        String query;
            query = "SELECT DISTINCT " + C_GRADE + "," + C_GRADE_NR + ""
                    + " FROM '" + TABLE_NAME_PAID + "'" + " WHERE " + C_GRADE
                    + " = '" + grade + "'  ORDER BY " + C_GRADE_NR + " ASC";

            Cursor mCursor = mDb.rawQuery(query, null);
            if (mCursor != null) {
                mCursor.moveToNext();
            }
            close();
            return mCursor;
    }

The crash is happening at the line mCursor.moveToNext();

Below is my log

03-31 12:27:42.433: E/AndroidRuntime(25885): android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=631 (# cursors opened by this proc=631)
03-31 12:27:42.433: E/AndroidRuntime(25885):    at android.database.CursorWindow.<init>(CursorWindow.java:108)
03-31 12:27:42.433: E/AndroidRuntime(25885):    at android.database.AbstractWindowedCursor.clearOrCreateWindow(AbstractWindowedCursor.java:198)
03-31 12:27:42.433: E/AndroidRuntime(25885):    at android.database.sqlite.SQLiteCursor.clearOrCreateWindow(SQLiteCursor.java:316)
03-31 12:27:42.433: E/AndroidRuntime(25885):    at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:142)
03-31 12:27:42.433: E/AndroidRuntime(25885):    at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:136)
03-31 12:27:42.433: E/AndroidRuntime(25885):    at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:197)
03-31 12:27:42.433: E/AndroidRuntime(25885):    at android.database.AbstractCursor.moveToNext(AbstractCursor.java:245)
03-31 12:27:42.433: E/AndroidRuntime(25885):    at com.tss.in.database.ISOFitsProvider.fetchAllFreeGradeNr(ISProvider.java:151)

Also tried as below

public Cursor fetchAllFreeGradeNr(String grade) {
        //Cursor mCursor ;
        open();
        String query;
            query = "SELECT DISTINCT " + C_GRADE + "," + C_GRADE_NR + ""
                    + " FROM '" + TABLE_NAME_PAID + "'" + " WHERE " + C_GRADE
                    + " = '" + grade + "'  ORDER BY " + C_GRADE_NR + " ASC";
        Cursor cursor = null;
        try {
            cursor = mDb.rawQuery(query, null);
            if (cursor != null) {
                return cursor;
            }
        } catch (Exception e) {
            if (cursor != null) {
                cursor.close();
            }
        }

        return cursor;
}

The above code also gives crash.

Upvotes: 1

Views: 1069

Answers (2)

user5703518
user5703518

Reputation:

you should close the cursor when you have finished with it, I mean when you are fetching data from it and populating your model then you must close it, So your method may be:

public Cursor fetchAllFreeGradeNr(String grade) {
        open();
        String query;
            query = "SELECT DISTINCT " + C_GRADE + "," + C_GRADE_NR + ""
                    + " FROM '" + TABLE_NAME_PAID + "'" + " WHERE " + C_GRADE
                    + " = '" + grade + "'  ORDER BY " + C_GRADE_NR + " ASC";

            Cursor mCursor = mDb.rawQuery(query, null);
            return mCursor;
    }

then in other method for example you have something like this:

   if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        do {
            //... retrieve data
        } while (cursor.moveToNext());
    }

    if(cursor != null) cursor.close();

Upvotes: 1

Yasir Tahir
Yasir Tahir

Reputation: 800

Most often the cause for this error are non closed cursors. Make sure you close all cursors after using them

if(mCursor!= null) mCursor.close();

Upvotes: 0

Related Questions