title: How to fix (android.database.CursorWindowAllocationException) crash
body: <p>Before the Local Datastore feature in Parse was available, I used to have my own local database for offline app usage. I think everything worked well until I decided to add a homescreen widget to my app, with its service and a listview that was fed from that local DB. After a while I started to get some errors from users(Cursor Allocation, # Opened Cursors), I thought it was something minor, maybe due to phone issues,etc,  but then I myself started experiencing them...I tried to tackle them in every possible manner suggested in SO, various types of solutions, trying to close everything pointing to the DB, try/catch stuff, etc, nothing. So after some months, I gave up and said to myself, that's it, going to use the Local DataStore from Parse, which I'm sure it's better than my sqlite implementation. </p>
  
  <p>This was maybe a month ago,and I thought things were solved, but today got this error, that appears even without the app being used. I stopped the apps and services, reboot/power off, but it is still there just when the phone unlocks and since this afternoon can't open the app because of it, it is constant.</p>
  
  <p>I don't know how to fix it, it's been a 4 months nightmare. Hoping for a helping hand, thanks.</p>
  
  <pre><code>07-21 19:01:29.360: E/CursorWindow(17106): Could not allocate CursorWindow '/data/data/com.xoaquin.r07d/databases/ParseOfflineStore' of size 2097152 due to error -12.
  07-21 19:01:29.470: E/CursorWindow(17106): Could not allocate CursorWindow '/data/data/com.xoaquin.r07d/databases/ParseOfflineStore' of size 2097152 due to error -12.
  07-21 19:01:29.480: E/AndroidRuntime(17106): FATAL EXCEPTION: main
  07-21 19:01:29.480: E/AndroidRuntime(17106): Process: com.xoaquin.r07d, PID: 17106
  07-21 19:01:29.480: E/AndroidRuntime(17106): java.lang.RuntimeException: Unable to create application com.xoaquin.r07d.DefaultApplication: android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=582 (# cursors opened by this proc=582)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4471)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at android.app.ActivityThread.access$1500(ActivityThread.java:145)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1266)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at android.os.Handler.dispatchMessage(Handler.java:102)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at android.os.Looper.loop(Looper.java:136)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at android.app.ActivityThread.main(ActivityThread.java:5141)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at java.lang.reflect.Method.invokeNative(Native Method)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at java.lang.reflect.Method.invoke(Method.java:515)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at dalvik.system.NativeStart.main(Native Method)
  07-21 19:01:29.480: E/AndroidRuntime(17106): Caused by: android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=582 (# cursors opened by this proc=582)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at android.database.CursorWindow.&lt;init&gt;(CursorWindow.java:104)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at android.database.AbstractWindowedCursor.clearOrCreateWindow(AbstractWindowedCursor.java:198)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:139)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at com.parse.ParseSQLiteDatabase$12.then(ParseSQLiteDatabase.java:214)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at com.parse.ParseSQLiteDatabase$12.then(ParseSQLiteDatabase.java:205)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at com.parse.Task$11.run(Task.java:481)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at com.parse.Task$ImmediateExecutor.execute(Task.java:673)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at com.parse.Task.completeAfterTask(Task.java:477)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at com.parse.Task.continueWithTask(Task.java:353)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at com.parse.Task.continueWithTask(Task.java:364)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at com.parse.Task$9.then(Task.java:410)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at com.parse.Task$9.then(Task.java:402)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at com.parse.Task$11.run(Task.java:481)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  07-21 19:01:29.480: E/AndroidRuntime(17106):    at java.lang.Thread.run(Thread.java:841)
  07-21 19:01:29.551: E/CursorWindow(17106): Could not allocate CursorWindow '/data/data/com.xoaquin.r07d/databases/ParseOfflineStore' of size 2097152 due to error -12.
  07-21 19:01:38.119: E/WindowManager(674): Starting window AppWindowToken{4312f298 token=Token{42bd2558 ActivityRecord{42538230 u0 com.xoaquin.r07d/.MainActivity t57}}} timed out
  </code></pre>
  
  <p><strong>Below is the error that I got when I was using my own sqlite implementation, before changing to the Parse Local Datastore:</strong> </p>
  
  <pre><code>java.lang.RuntimeException: Unable to create application com.xoaquin.r07d.DefaultApplication: android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=762 (# cursors opened by this proc=762)
  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4428)
  at android.app.ActivityThread.access$1500(ActivityThread.java:139)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1270)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:136)
  at android.app.ActivityThread.main(ActivityThread.java:5102)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:515)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  at dalvik.system.NativeStart.main(Native Method)
  Caused by: android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=762 (# cursors opened by this proc=762)
  at android.database.CursorWindow.&lt;init&gt;(CursorWindow.java:104)
  at android.database.AbstractWindowedCursor.clearOrCreateWindow(AbstractWindowedCursor.java:198)
  at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:139)
  at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133)
  at com.parse.ParseSQLiteDatabase$12.then(ParseSQLiteDatabase.java:214)
  at com.parse.ParseSQLiteDatabase$12.then(ParseSQLiteDatabase.java:205)
  at com.parse.Task$11.run(Task.java:481)
  at com.parse.Task$ImmediateExecutor.execute(Task.java:673)
  at com.parse.Task.completeAfterTask(Task.java:477)
  at com.parse.Task.continueWithTask(Task.java:353)
  at com.parse.Task.continueWithTask(Task.java:364)
  at com.parse.Task$9.then(Task.java:410)
  at com.parse.Task$9.then(Task.java:402)
  at com.parse.Task$11.run(Task.java:481)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  at java.lang.Thread.run(Thread.java:841)
  </code></pre>
  
  <p><strong>Below the code for my application class which <em>is the only class of my own code</em> that is mentioned in the errors(maybe there's something different that is wrong in there):</strong></p>
  
  <pre><code>public class DefaultApplication extends Application{ 
  
  
      private static Context context; 
  
      @Override
      public void onCreate() {
          super.onCreate();
  
          String manufacturer = Build.MANUFACTURER;
          String brand        = Build.BRAND;
          String product      = Build.PRODUCT;
          String model        = Build.MODEL;
  
          Parse.enableLocalDatastore(this);
          Parse.initialize(this, "KfBj6ivkLAaYqo", "ePZ6T7RmvGGw3"); 
  
  
  
  
              PushService.setDefaultPushCallback(this, MainActivity.class);
  
              ParseFacebookUtils.initialize("75589");
  
              PushService.subscribe(this, "todos", MainActivity.class);
  
              ParseUser cu = ParseUser.getCurrentUser(); 
              if(cu!=null){
  
              String nombretablausuario=cu.getUsername();  
  
              if(nombretablausuario!=null){
  
                  if (nombretablausuario.contains("\\.")||nombretablausuario.contains("@")){
                            nombretablausuario=nombretablausuario.replaceAll("\\.", "");
                            nombretablausuario=nombretablausuario.replaceAll("@", "");    
                        }
              PushService.subscribe(this, nombretablausuario,MainActivity.class); 
  
              }
  
              ParseInstallation installation = ParseInstallation.getCurrentInstallation();
              installation.put("actualuser",cu.getUsername());
              installation.put("manufacturer", manufacturer);
              installation.put("brand", brand);
              installation.put("product", product);
              installation.put("model", model);
              installation.saveEventually();
  
              }
  
              String locale = getResources().getConfiguration().locale.getDisplayName();
  
              if(locale.contains("espa\u00F1ol")){ 
  
                  PushService.subscribe(this, "jalert", JalertActivity.class);
                  PushService.unsubscribe(this, "jalerting");
              }else{
                  PushService.subscribe(this, "jalerting", JalertActivity.class);
                  PushService.unsubscribe(this, "jalert");
              }
  
               context=getApplicationContext();
  
      }
  
  
      public static Context getCustomAppContext(){ 
        return context;
      }
  
  
  }
  </code></pre>