Avoid leaking cursors
Mattias Nilsson [Thu, 27 Mar 2014 15:20:21 +0000 (16:20 +0100)]
Adding try/finally blocks to make sure that cursor
resources are let go

Change-Id: I596074aa9ab5752f91a26b5a03e1f39c23c64a5f

src/com/android/providers/downloads/DownloadProvider.java

index 45af445..48350f6 100644 (file)
@@ -1045,12 +1045,16 @@ public final class DownloadProvider extends ContentProvider {
             filteredValues = values;
             String filename = values.getAsString(Downloads.Impl._DATA);
             if (filename != null) {
-                Cursor c = query(uri, new String[]
-                        { Downloads.Impl.COLUMN_TITLE }, null, null, null);
-                if (!c.moveToFirst() || c.getString(0).isEmpty()) {
-                    values.put(Downloads.Impl.COLUMN_TITLE, new File(filename).getName());
+                Cursor c = null;
+                try {
+                    c = query(uri, new String[]
+                            { Downloads.Impl.COLUMN_TITLE }, null, null, null);
+                    if (!c.moveToFirst() || c.getString(0).isEmpty()) {
+                        values.put(Downloads.Impl.COLUMN_TITLE, new File(filename).getName());
+                    }
+                } finally {
+                    IoUtils.closeQuietly(c);
                 }
-                c.close();
             }
 
             Integer status = values.getAsInteger(Downloads.Impl.COLUMN_STATUS);
@@ -1269,29 +1273,35 @@ public final class DownloadProvider extends ContentProvider {
         if (cursor == null) {
             Log.v(Constants.TAG, "null cursor in openFile");
         } else {
-            if (!cursor.moveToFirst()) {
-                Log.v(Constants.TAG, "empty cursor in openFile");
-            } else {
-                do {
-                    Log.v(Constants.TAG, "row " + cursor.getInt(0) + " available");
-                } while(cursor.moveToNext());
+            try {
+                if (!cursor.moveToFirst()) {
+                    Log.v(Constants.TAG, "empty cursor in openFile");
+                } else {
+                    do {
+                        Log.v(Constants.TAG, "row " + cursor.getInt(0) + " available");
+                    } while(cursor.moveToNext());
+                }
+            } finally {
+                cursor.close();
             }
-            cursor.close();
         }
         cursor = query(uri, new String[] { "_data" }, null, null, null);
         if (cursor == null) {
             Log.v(Constants.TAG, "null cursor in openFile");
         } else {
-            if (!cursor.moveToFirst()) {
-                Log.v(Constants.TAG, "empty cursor in openFile");
-            } else {
-                String filename = cursor.getString(0);
-                Log.v(Constants.TAG, "filename in openFile: " + filename);
-                if (new java.io.File(filename).isFile()) {
-                    Log.v(Constants.TAG, "file exists in openFile");
+            try {
+                if (!cursor.moveToFirst()) {
+                    Log.v(Constants.TAG, "empty cursor in openFile");
+                } else {
+                    String filename = cursor.getString(0);
+                    Log.v(Constants.TAG, "filename in openFile: " + filename);
+                    if (new java.io.File(filename).isFile()) {
+                        Log.v(Constants.TAG, "file exists in openFile");
+                    }
                 }
+            } finally {
+                cursor.close();
             }
-            cursor.close();
         }
     }