From 89c74e89e52e3d3223ec9c2f108a9b0735d99d33 Mon Sep 17 00:00:00 2001 From: "Yoohyun.choi" Date: Wed, 6 Mar 2013 10:08:58 +0900 Subject: [PATCH] avoid cursor leak in email provider When cursor is done, the cursor must be closed to avoid cursor leak. cherry-pick of https://android-review.googlesource.com/#/c/52910/ Change-Id: Iaaffed2c4e90dc4b17ae55fcedb242bf890b3dd5 --- src/com/android/email/provider/DBHelper.java | 24 ++++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/com/android/email/provider/DBHelper.java b/src/com/android/email/provider/DBHelper.java index 219ba16c7..bda0a9eb7 100644 --- a/src/com/android/email/provider/DBHelper.java +++ b/src/com/android/email/provider/DBHelper.java @@ -1066,16 +1066,20 @@ public final class DBHelper { Cursor c = db.query(Account.TABLE_NAME, new String[] {EmailContent.RECORD_ID /*0*/, AccountColumns.SECURITY_FLAGS /*1*/}, AccountColumns.SECURITY_FLAGS + ">0", null, null, null, null); - ContentValues cv = new ContentValues(); - String[] args = new String[1]; - while (c.moveToNext()) { - long securityFlags = c.getLong(1 /*SECURITY_FLAGS*/); - Policy policy = LegacyPolicySet.flagsToPolicy(securityFlags); - long policyId = db.insert(Policy.TABLE_NAME, null, policy.toContentValues()); - cv.put(AccountColumns.POLICY_KEY, policyId); - cv.putNull(AccountColumns.SECURITY_FLAGS); - args[0] = Long.toString(c.getLong(0 /*RECORD_ID*/)); - db.update(Account.TABLE_NAME, cv, EmailContent.RECORD_ID + "=?", args); + try { + ContentValues cv = new ContentValues(); + String[] args = new String[1]; + while (c.moveToNext()) { + long securityFlags = c.getLong(1 /*SECURITY_FLAGS*/); + Policy policy = LegacyPolicySet.flagsToPolicy(securityFlags); + long policyId = db.insert(Policy.TABLE_NAME, null, policy.toContentValues()); + cv.put(AccountColumns.POLICY_KEY, policyId); + cv.putNull(AccountColumns.SECURITY_FLAGS); + args[0] = Long.toString(c.getLong(0 /*RECORD_ID*/)); + db.update(Account.TABLE_NAME, cv, EmailContent.RECORD_ID + "=?", args); + } + } finally { + c.close(); } }