irccloud/android

resource leak: database cursor not closed

andrew659 opened this issue · 0 comments

Dear developers,

I found two potential leaks of database cursor in the onCreate() method of the IRCCloudApplicationBase class:

https://github.com/irccloud/android/blob/master/src/com/irccloud/android/IRCCloudApplicationBase.java

(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.