Skip to content

Commit f6ae910

Browse files
committed
More db resource leaks
1 parent 19a346b commit f6ae910

3 files changed

Lines changed: 49 additions & 30 deletions

File tree

app/src/org/gnucash/android/db/AccountsDbAdapter.java

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,13 @@ public long getAccountID(String uid){
269269
new String[] {DatabaseHelper.KEY_ROW_ID, DatabaseHelper.KEY_UID},
270270
DatabaseHelper.KEY_UID + " = '" + uid + "'", null, null, null, null);
271271
long result = -1;
272-
if (cursor != null && cursor.moveToFirst()){
273-
Log.v(TAG, "Returning account id");
274-
result = cursor.getLong(DatabaseAdapter.COLUMN_ROW_ID);
275-
276-
cursor.close();
277-
}
272+
if (cursor != null) {
273+
if (cursor.moveToFirst()) {
274+
Log.v(TAG, "Returning account id");
275+
result = cursor.getLong(DatabaseAdapter.COLUMN_ROW_ID);
276+
}
277+
cursor.close();
278+
}
278279
return result;
279280
}
280281

@@ -291,12 +292,13 @@ public String getParentAccountUID(String uid){
291292
new String[]{uid},
292293
null, null, null, null);
293294
String result = null;
294-
if (cursor != null && cursor.moveToFirst()){
295-
Log.d(TAG, "Account already exists. Returning existing id");
296-
result = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.KEY_PARENT_ACCOUNT_UID));
297-
298-
cursor.close();
299-
}
295+
if (cursor != null) {
296+
if (cursor.moveToFirst()) {
297+
Log.d(TAG, "Account already exists. Returning existing id");
298+
result = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.KEY_PARENT_ACCOUNT_UID));
299+
}
300+
cursor.close();
301+
}
300302
return result;
301303
}
302304

@@ -320,10 +322,12 @@ public Account getAccount(long rowId){
320322
Account account = null;
321323
Log.v(TAG, "Fetching account with id " + rowId);
322324
Cursor c = fetchRecord(DatabaseHelper.ACCOUNTS_TABLE_NAME, rowId);
323-
if (c != null && c.moveToFirst()){
324-
account = buildAccountInstance(c);
325-
c.close();
326-
}
325+
if (c != null) {
326+
if (c.moveToFirst()) {
327+
account = buildAccountInstance(c);
328+
}
329+
c.close();
330+
}
327331
return account;
328332
}
329333

@@ -348,10 +352,12 @@ public String getAccountUID(long id){
348352
new String[]{DatabaseHelper.KEY_ROW_ID, DatabaseHelper.KEY_UID},
349353
DatabaseHelper.KEY_ROW_ID + "=" + id,
350354
null, null, null, null);
351-
if (c != null && c.moveToFirst()){
352-
uid = c.getString(c.getColumnIndexOrThrow(DatabaseHelper.KEY_UID));
353-
c.close();
354-
}
355+
if (c != null) {
356+
if (c.moveToFirst()) {
357+
uid = c.getString(c.getColumnIndexOrThrow(DatabaseHelper.KEY_UID));
358+
}
359+
c.close();
360+
}
355361
return uid;
356362
}
357363

@@ -366,8 +372,10 @@ public String getAccountColorCode(long accountId){
366372
new String[]{DatabaseHelper.KEY_ROW_ID, DatabaseHelper.KEY_COLOR_CODE},
367373
DatabaseHelper.KEY_ROW_ID + "=" + accountId,
368374
null, null, null, null);
369-
if (c != null && c.moveToFirst()){
370-
colorCode = c.getString(c.getColumnIndexOrThrow(DatabaseHelper.KEY_COLOR_CODE));
375+
if (c != null) {
376+
if (c.moveToFirst()) {
377+
colorCode = c.getString(c.getColumnIndexOrThrow(DatabaseHelper.KEY_COLOR_CODE));
378+
}
371379
c.close();
372380
}
373381
return colorCode;
@@ -399,10 +407,12 @@ public AccountType getAccountType(long accountId){
399407
public String getName(long accountID) {
400408
String name = null;
401409
Cursor c = fetchRecord(DatabaseHelper.ACCOUNTS_TABLE_NAME, accountID);
402-
if (c != null && c.moveToFirst()){
403-
name = c.getString(DatabaseAdapter.COLUMN_NAME);
404-
c.close();
405-
}
410+
if (c != null) {
411+
if (c.moveToFirst()) {
412+
name = c.getString(DatabaseAdapter.COLUMN_NAME);
413+
}
414+
c.close();
415+
}
406416
return name;
407417
}
408418

app/src/org/gnucash/android/ui/transaction/TransactionsActivity.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ public class TransactionsActivity extends SherlockFragmentActivity implements
116116
*/
117117
private AccountsDbAdapter mAccountsDbAdapter;
118118

119+
/**
120+
* Hold the accounts cursor that will be used in the Navigation
121+
*/
122+
private Cursor mAccountsCursor = null;
123+
119124
/**
120125
* This is the last known color for the title indicator.
121126
* This is used to remember the color of the top level account if the child account doesn't have one.
@@ -348,19 +353,22 @@ private void setTitleIndicatorColor() {
348353
*/
349354
private void setupActionBarNavigation() {
350355
// set up spinner adapter for navigation list
351-
Cursor accountsCursor = mAccountsDbAdapter.fetchAllRecordsOrderedByFullName();
356+
if (mAccountsCursor != null) {
357+
mAccountsCursor.close();
358+
}
359+
mAccountsCursor = mAccountsDbAdapter.fetchAllRecordsOrderedByFullName();
352360

353361
SpinnerAdapter mSpinnerAdapter = new QualifiedAccountNameCursorAdapter(
354362
getSupportActionBar().getThemedContext(),
355-
R.layout.sherlock_spinner_item, accountsCursor);
363+
R.layout.sherlock_spinner_item, mAccountsCursor);
356364
((ResourceCursorAdapter) mSpinnerAdapter)
357365
.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
358366
ActionBar actionBar = getSupportActionBar();
359367
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
360368
actionBar.setListNavigationCallbacks(mSpinnerAdapter,
361369
mTransactionListNavigationListener);
362370
actionBar.setDisplayHomeAsUpEnabled(true);
363-
371+
364372
updateNavigationSelection();
365373
}
366374

@@ -449,6 +457,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
449457
@Override
450458
protected void onDestroy() {
451459
super.onDestroy();
460+
mAccountsCursor.close();
452461
mAccountsDbAdapter.close();
453462
}
454463

app/src/org/gnucash/android/util/GnucashAccountXmlHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public static void parse(Context context, InputStream accountsInputStream){
202202
GnucashAccountXmlHandler handler = new GnucashAccountXmlHandler(context);
203203
xr.setContentHandler(handler);
204204
xr.parse(new InputSource(bos));
205-
205+
handler.mDatabaseAdapter.close();
206206
} catch (Exception e) {
207207
e.printStackTrace();
208208
Toast.makeText(context, R.string.toast_error_importing_accounts, Toast.LENGTH_LONG).show();

0 commit comments

Comments
 (0)