resource leak: database cursor not closed
andrew659 opened this issue · 0 comments
andrew659 commented
Dear developers,
I found two potential leaks of database cursor in the onCreate() method of the IRCCloudApplicationBase class:
(1) starting from line 94:
Cursor c = getContentResolver().query(
Uri.parse(notification_uri),
new String[]{MediaStore.Audio.Media.TITLE},
null,
null,
null);
if (c != null && c.moveToFirst()) {
if (c.getString(0).equals("IRCCloud")) {
Log.d("IRCCloud", "Migrating notification ringtone setting: " + notification_uri);
SharedPreferences.Editor editor = prefs.edit();
editor.remove("notify_ringtone");
editor.commit();
}
c.close();
}
In the above code, if the cursor is empty (moveToFirst() returns false), it will be left unclosed since only when c.moveToFirst() returns true the c.close() statement will execute.
(2) starting from line 131:
Cursor c = getContentResolver().query(
Uri.parse(notification_uri),
new String[]{MediaStore.Audio.Media.TITLE},
null,
null,
null);
if (c != null && c.moveToFirst()) {
if (c.getString(0).equals("IRCCloud")) {
Log.d("IRCCloud", "Migrating notification ringtone setting: " + notification_uri);
SharedPreferences.Editor editor = prefs.edit();
editor.remove("notify_ringtone");
editor.commit();
}
c.close();
}
The same issue will happen if the cursor is empty.
Is this a bug? Thanks.