From b8ff8ca9d9d17f61f3f0e019ed0b62fe13d1a33f Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Tue, 18 Feb 2014 16:02:51 +0900 Subject: Straighten out database cursors behavior. Some were never closed, other closed twice. This change makes all Cursor instances behave, having the #close() call in a finally{} clause, and puts the burden of closing the cursor squarely on the creator rather than in the called methods. There is however one exception that is beyond the scope of this change: UserDictionarySettings have a Cursor member, it's never closed, and fixing the problem is not obvious. This change adds a TODO for now. It's not very clear if this change actually helps with bug#12670151, but it may be related and it's a good think to do anyway. Bug: 12670151 Change-Id: I87cc44387e7dee3da1488671b93a28d9d73f7dc0 --- .../inputmethod/dictionarypack/MetadataHandler.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'java/src/com/android/inputmethod/dictionarypack/MetadataHandler.java') diff --git a/java/src/com/android/inputmethod/dictionarypack/MetadataHandler.java b/java/src/com/android/inputmethod/dictionarypack/MetadataHandler.java index a0147b6d6..5c2289911 100644 --- a/java/src/com/android/inputmethod/dictionarypack/MetadataHandler.java +++ b/java/src/com/android/inputmethod/dictionarypack/MetadataHandler.java @@ -44,8 +44,7 @@ public class MetadataHandler { */ private static List makeMetadataObject(final Cursor results) { final ArrayList buildingMetadata = new ArrayList(); - - if (results.moveToFirst()) { + if (null != results && results.moveToFirst()) { final int localeColumn = results.getColumnIndex(MetadataDbHelper.LOCALE_COLUMN); final int typeColumn = results.getColumnIndex(MetadataDbHelper.TYPE_COLUMN); final int descriptionColumn = @@ -61,7 +60,6 @@ public class MetadataHandler { final int versionIndex = results.getColumnIndex(MetadataDbHelper.VERSION_COLUMN); final int formatVersionIndex = results.getColumnIndex(MetadataDbHelper.FORMATVERSION_COLUMN); - do { buildingMetadata.add(new WordListMetadata(results.getString(idIndex), results.getInt(typeColumn), @@ -75,8 +73,6 @@ public class MetadataHandler { results.getInt(formatVersionIndex), 0, results.getString(localeColumn))); } while (results.moveToNext()); - - results.close(); } return Collections.unmodifiableList(buildingMetadata); } @@ -92,9 +88,14 @@ public class MetadataHandler { // If clientId is null, we get a cursor on the default database (see // MetadataDbHelper#getInstance() for more on this) final Cursor results = MetadataDbHelper.queryCurrentMetadata(context, clientId); - final List resultList = makeMetadataObject(results); - results.close(); - return resultList; + // If null, we should return makeMetadataObject(null), so we go through. + try { + return makeMetadataObject(results); + } finally { + if (null != results) { + results.close(); + } + } } /** -- cgit v1.2.3-83-g751a