Skip to content

Commit e0fe69a

Browse files
fefe982codinguser
authored andcommitted
FIX: Cursor leak
Cursor leaks would cause the cursor resource to be exhausted when importing a large account tree with a lot of transactions.
1 parent 74cde45 commit e0fe69a

2 files changed

Lines changed: 18 additions & 10 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,8 +736,10 @@ public String getGnuCashRootAccountUID(){
736736
String condition = AccountEntry.COLUMN_TYPE + "= '" + AccountType.ROOT.name() + "'";
737737
Cursor cursor = fetchAccounts(condition);
738738
String rootUID = null;
739-
if (cursor != null && cursor.moveToFirst()){
740-
rootUID = cursor.getString(cursor.getColumnIndexOrThrow(AccountEntry.COLUMN_UID));
739+
if (cursor != null) {
740+
if (cursor.moveToFirst()) {
741+
rootUID = cursor.getString(cursor.getColumnIndexOrThrow(AccountEntry.COLUMN_UID));
742+
}
741743
cursor.close();
742744
}
743745
return rootUID;

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,10 @@ public Split getSplit(long id){
126126
Cursor cursor = fetchRecord(id);
127127

128128
Split split = null;
129-
if (cursor != null && cursor.moveToFirst()){
130-
split = buildSplitInstance(cursor);
129+
if (cursor != null) {
130+
if (cursor.moveToFirst()) {
131+
split = buildSplitInstance(cursor);
132+
}
131133
cursor.close();
132134
}
133135
return split;
@@ -248,10 +250,11 @@ public long getID(String uid){
248250
new String[] {SplitEntry._ID},
249251
SplitEntry.COLUMN_UID + " = ?", new String[]{uid}, null, null, null);
250252
long result = -1;
251-
if (cursor != null && cursor.moveToFirst()){
252-
Log.d(TAG, "Transaction already exists. Returning existing id");
253-
result = cursor.getLong(cursor.getColumnIndexOrThrow(SplitEntry._ID));
254-
253+
if (cursor != null){
254+
if (cursor.moveToFirst()) {
255+
Log.d(TAG, "Transaction already exists. Returning existing id");
256+
result = cursor.getLong(cursor.getColumnIndexOrThrow(SplitEntry._ID));
257+
}
255258
cursor.close();
256259
}
257260
return result;
@@ -347,8 +350,11 @@ public String getTransactionUID(long transactionId){
347350
null, null, null, null);
348351

349352
String trxUID = null;
350-
if (cursor != null && cursor.moveToFirst()){
351-
trxUID = cursor.getString(cursor.getColumnIndexOrThrow(TransactionEntry.COLUMN_UID));
353+
if (cursor != null) {
354+
if (cursor.moveToFirst()) {
355+
trxUID = cursor.getString(cursor.getColumnIndexOrThrow(TransactionEntry.COLUMN_UID));
356+
}
357+
cursor.close();
352358
}
353359

354360
return trxUID;

0 commit comments

Comments
 (0)