Skip to content
This repository was archived by the owner on Aug 19, 2021. It is now read-only.

Commit d9d6067

Browse files
author
Michael Farb
committed
major database cursor management cleanup
- massive cursor management cleanup correcting resource leaks and handful of errors missing the first row in the cursor
1 parent 11f405c commit d9d6067

12 files changed

Lines changed: 1089 additions & 806 deletions

safeslinger-messenger/src/edu/cmu/cylab/starslinger/SafeSlinger.java

Lines changed: 179 additions & 148 deletions
Large diffs are not rendered by default.

safeslinger-messenger/src/edu/cmu/cylab/starslinger/model/ContactAccessorApi5.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -754,16 +754,18 @@ public boolean updateOldContact(ContactStruct contact, Activity act, String sele
754754
public boolean updateDataRow(Context ctx, String[] proj, String where, String[] args,
755755
ContentValues values) {
756756
Cursor c = ctx.getContentResolver().query(Data.CONTENT_URI, proj, where, args, null);
757-
758-
if (c.moveToFirst()) {
759-
// remove old, including dupes
760-
if (ctx.getContentResolver().delete(Data.CONTENT_URI, where, args) == 0) {
757+
if (c != null) {
758+
try {
759+
if (c.moveToFirst()) {
760+
// remove old, including dupes
761+
if (ctx.getContentResolver().delete(Data.CONTENT_URI, where, args) == 0) {
762+
return false; // error
763+
}
764+
}
765+
} finally {
761766
c.close();
762-
return false; // error
763767
}
764768
}
765-
c.close();
766-
767769
if (ctx.getContentResolver().insert(Data.CONTENT_URI, values) != null)
768770
return true; // inserted a new entry or updated entry
769771
return false; // error

safeslinger-messenger/src/edu/cmu/cylab/starslinger/model/InboxDbAdapter.java

Lines changed: 53 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,6 @@ private Cursor query(String table, String[] columns, String selection, String[]
109109
String groupBy, String having, String orderBy) {
110110
Cursor query = mDatabase.query(table, columns, selection, selectionArgs, groupBy, having,
111111
orderBy);
112-
// if (query != null) {
113-
// MyLog.d(TAG, query.getCount() + " " + table + " " + selection + " " +
114-
// selectionArgs
115-
// + " " + groupBy + " " + having + " " + orderBy);
116-
// }
117112
return query;
118113
}
119114

@@ -128,11 +123,6 @@ private Cursor query(boolean distinct, String table, String[] columns, String se
128123
String[] selectionArgs, String groupBy, String having, String orderBy, String limit) {
129124
Cursor query = mDatabase.query(distinct, table, columns, selection, selectionArgs, groupBy,
130125
having, orderBy, limit);
131-
// if (query != null) {
132-
// MyLog.d(TAG, query.getCount() + " " + table + " " + selection + " " +
133-
// selectionArgs
134-
// + " " + groupBy + " " + having + " " + orderBy + " " + limit);
135-
// }
136126
return query;
137127
}
138128

@@ -145,11 +135,13 @@ public long createRecvEncInbox(String msgHash, int status, int seen) {
145135
MessageDbAdapter.KEY_MSGHASH
146136
}, where, null, null, null, null, null);
147137
if (c != null) {
148-
if (c.getCount() > 0) {
138+
try {
139+
if (c.getCount() > 0) {
140+
return -1;
141+
}
142+
} finally {
149143
c.close();
150-
return -1;
151144
}
152-
c.close();
153145
}
154146
ContentValues values = new ContentValues();
155147
values.put(MessageDbAdapter.KEY_DATE_RECV, System.currentTimeMillis()); // Received
@@ -235,12 +227,7 @@ public Cursor fetchInboxSmall(long rowId) throws SQLException {
235227
MessageDbAdapter.KEY_RETNOTIFY, MessageDbAdapter.KEY_RETPUSHTOKEN,
236228
MessageDbAdapter.KEY_RETRECEIPT
237229
}, where, null, null, null, null, null);
238-
if (c != null) {
239-
if (c.moveToFirst()) {
240-
return c;
241-
}
242-
}
243-
return null;
230+
return c;
244231
}
245232
}
246233

@@ -261,9 +248,12 @@ public int getAllInboxCountByThread(String keyId) {
261248
}
262249
Cursor c = query(DATABASE_TABLE, null, where.toString(), null, null, null, null);
263250
if (c != null) {
264-
int count = c.getCount();
265-
c.close();
266-
return count;
251+
try {
252+
int count = c.getCount();
253+
return count;
254+
} finally {
255+
c.close();
256+
}
267257
}
268258
return -1;
269259
}
@@ -291,10 +281,7 @@ public Cursor fetchAllInboxGetMessagePending() {
291281
MessageDbAdapter.KEY_RETNOTIFY, MessageDbAdapter.KEY_RETPUSHTOKEN,
292282
MessageDbAdapter.KEY_RETRECEIPT
293283
}, where.toString(), null, null, null, null, null);
294-
if (c != null) {
295-
return c;
296-
}
297-
return null;
284+
return c;
298285
}
299286
}
300287

@@ -322,10 +309,7 @@ public Cursor fetchAllInboxDecryptPending() {
322309
MessageDbAdapter.KEY_RETNOTIFY, MessageDbAdapter.KEY_RETPUSHTOKEN,
323310
MessageDbAdapter.KEY_RETRECEIPT
324311
}, where.toString(), null, null, null, null, null);
325-
if (c != null) {
326-
return c;
327-
}
328-
return null;
312+
return c;
329313
}
330314
}
331315

@@ -357,10 +341,7 @@ public Cursor fetchAllInboxByThread(String keyId) {
357341
MessageDbAdapter.KEY_RETNOTIFY, MessageDbAdapter.KEY_RETPUSHTOKEN,
358342
MessageDbAdapter.KEY_RETRECEIPT
359343
}, where.toString(), null, null, null, null, null);
360-
if (c != null) {
361-
return c;
362-
}
363-
return null;
344+
return c;
364345
}
365346
}
366347

@@ -370,16 +351,21 @@ public long fetchLastRecentMessageTime() {
370351
* SELECT MessageDbAdapter.KEY_ROWID, MessageDbAdapter.KEY_DATE_RECV
371352
* FROM table ORDER BY MessageDbAdapter.KEY_ROWID DESC LIMIT 1;
372353
*/
373-
Cursor cursor = query(DATABASE_TABLE, new String[] {
354+
Cursor c = query(DATABASE_TABLE, new String[] {
374355
MessageDbAdapter.KEY_ROWID, MessageDbAdapter.KEY_DATE_RECV
375356
}, null, null, null, null, MessageDbAdapter.KEY_ROWID + " DESC", "1");
376-
if (cursor != null && cursor.getCount() > 0) {
377-
cursor.moveToFirst();
378-
long time = cursor.getLong(cursor.getColumnIndex(MessageDbAdapter.KEY_DATE_RECV));
379-
return time;
357+
if (c != null) {
358+
try {
359+
if (c.moveToFirst()) {
360+
long time = c.getLong(c.getColumnIndex(MessageDbAdapter.KEY_DATE_RECV));
361+
return time;
362+
}
363+
} finally {
364+
c.close();
365+
}
380366
}
367+
return -1;
381368
}
382-
return -1;
383369
}
384370

385371
public Cursor fetchInboxRecent(String keyId) {
@@ -411,10 +397,7 @@ public Cursor fetchInboxRecent(String keyId) {
411397
MessageDbAdapter.KEY_RETNOTIFY, MessageDbAdapter.KEY_RETPUSHTOKEN,
412398
MessageDbAdapter.KEY_RETRECEIPT
413399
}, where.toString(), null, groupBy, null, null);
414-
if (c != null) {
415-
return c;
416-
}
417-
return null;
400+
return c;
418401
}
419402
}
420403

@@ -434,10 +417,7 @@ public Cursor fetchInboxRecentByUniqueKeyIds() {
434417
MessageDbAdapter.KEY_RETNOTIFY, MessageDbAdapter.KEY_RETPUSHTOKEN,
435418
MessageDbAdapter.KEY_RETRECEIPT
436419
}, null, null, groupBy, null, null);
437-
if (c != null) {
438-
return c;
439-
}
440-
return null;
420+
return c;
441421
}
442422
}
443423

@@ -452,9 +432,12 @@ public int getUnseenInboxCount() throws SQLException {
452432
where.append(")");
453433
Cursor c = query(DATABASE_TABLE, null, where.toString(), null, null, null, null);
454434
if (c != null) {
455-
int count = c.getCount();
456-
c.close();
457-
return count;
435+
try {
436+
int count = c.getCount();
437+
return count;
438+
} finally {
439+
c.close();
440+
}
458441
}
459442
return -1;
460443
}
@@ -486,9 +469,12 @@ public int getActionRequiredInboxCountByThread(String keyId) throws SQLException
486469
where.append(")");
487470
Cursor c = query(DATABASE_TABLE, null, where.toString(), null, null, null, null);
488471
if (c != null) {
489-
int count = c.getCount();
490-
c.close();
491-
return count;
472+
try {
473+
int count = c.getCount();
474+
return count;
475+
} finally {
476+
c.close();
477+
}
492478
}
493479
return -1;
494480
}
@@ -518,13 +504,20 @@ public void updateAllInboxAsSeenByThread(String keyId) throws SQLException {
518504
MessageDbAdapter.KEY_KEYID
519505
}, where.toString(), null, null, null, null);
520506
if (c != null) {
521-
ContentValues values = new ContentValues();
522-
values.put(MessageDbAdapter.KEY_SEEN, MessageDbAdapter.MESSAGE_IS_SEEN); // seen
523-
while (c.moveToNext()) {
524-
long rowId = c.getLong(c.getColumnIndexOrThrow(MessageDbAdapter.KEY_ROWID));
525-
update(DATABASE_TABLE, values, MessageDbAdapter.KEY_ROWID + "=" + rowId, null);
507+
try {
508+
if (c.moveToFirst()) {
509+
ContentValues values = new ContentValues();
510+
values.put(MessageDbAdapter.KEY_SEEN, MessageDbAdapter.MESSAGE_IS_SEEN); // seen
511+
do {
512+
long rowId = c.getLong(c
513+
.getColumnIndexOrThrow(MessageDbAdapter.KEY_ROWID));
514+
update(DATABASE_TABLE, values,
515+
MessageDbAdapter.KEY_ROWID + "=" + rowId, null);
516+
} while (c.moveToNext());
517+
}
518+
} finally {
519+
c.close();
526520
}
527-
c.close();
528521
}
529522
}
530523
}

0 commit comments

Comments
 (0)