Skip to content

Commit f735046

Browse files
committed
fix few minor memory leaks
1 parent abf237c commit f735046

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

src/cloudsync.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1529,7 +1529,6 @@ void cloudsync_context_free (void *ptr) {
15291529
cloudsync_context *data = (cloudsync_context*)ptr;
15301530
cloudsync_memory_free(data->tables);
15311531
cloudsync_memory_free(data);
1532-
cloudsync_memory_finalize();
15331532
}
15341533

15351534
const char *cloudsync_context_init (sqlite3 *db, cloudsync_context *data, sqlite3_context *context) {

src/dbutils.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,7 @@ int dbutils_update_schema_hash(sqlite3 *db, uint64_t *hash) {
10621062
if (!schema) return SQLITE_ERROR;
10631063

10641064
sqlite3_uint64 h = fnv1a_hash(schema, strlen(schema));
1065+
cloudsync_memory_free(schema);
10651066
if (hash && *hash == h) return SQLITE_CONSTRAINT;
10661067

10671068
char sql[1024];

src/vtab.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ int cloudsync_vtab_set_error (sqlite3_vtab *vtab, const char *format, ...) {
500500
char *err = cloudsync_memory_vmprintf(format, arg);
501501
va_end (arg);
502502

503+
if (vtab->zErrMsg) cloudsync_memory_free(vtab->zErrMsg);
503504
vtab->zErrMsg = err;
504505
return SQLITE_ERROR;
505506
}

test/unit.c

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ sqlite3 *close_db (sqlite3 *db) {
253253
if (db) {
254254
sqlite3_exec(db, "SELECT cloudsync_terminate();", NULL, NULL, NULL);
255255
dbutils_debug_stmt(db, true);
256-
sqlite3_close(db);
256+
int rc = sqlite3_close(db);
257+
if (rc != SQLITE_OK) printf("Error while closing db (%d)\n", rc);
257258
}
258259
return NULL;
259260
}
@@ -334,6 +335,7 @@ int unittest_payload_apply_reset_transaction(sqlite3 *db, unittest_payload_apply
334335
}
335336

336337
bool unittest_payload_apply_rls_callback(void **xdata, cloudsync_pk_decode_bind_context *d, sqlite3 *db, cloudsync_context *data, int step, int rc) {
338+
bool is_approved = false;
337339
unittest_payload_apply_rls_status *s;
338340
if (*xdata) {
339341
s = (unittest_payload_apply_rls_status *)*xdata;
@@ -405,9 +407,12 @@ bool unittest_payload_apply_rls_callback(void **xdata, cloudsync_pk_decode_bind_
405407
s->last_db_version = db_version;
406408
s->is_approved = true;
407409
}
410+
411+
is_approved = s->is_approved;
408412
break;
409413
}
410414
case CLOUDSYNC_PAYLOAD_APPLY_DID_APPLY:
415+
is_approved = s->is_approved;
411416
break;
412417
case CLOUDSYNC_PAYLOAD_APPLY_CLEANUP:
413418
if (s->is_approved && !s->last_is_delete) s->is_approved = unittest_validate_changed_row(db, data, s->last_tbl, s->last_pk, s->last_pk_len);
@@ -417,13 +422,14 @@ bool unittest_payload_apply_rls_callback(void **xdata, cloudsync_pk_decode_bind_
417422
cloudsync_memory_free(s->last_pk);
418423
s->last_pk_len = 0;
419424
}
420-
425+
is_approved = s->is_approved;
426+
421427
cloudsync_memory_free(s);
422428
*xdata = NULL;
423429
break;
424430
}
425431

426-
return s->is_approved;
432+
return is_approved;
427433
}
428434
#endif
429435

@@ -870,6 +876,7 @@ bool do_test_vtab2 (void) {
870876

871877
finalize:
872878
if (rc != SQLITE_OK) printf("do_test_vtab2 error: %s\n", sqlite3_errmsg(db));
879+
db = close_db(db);
873880
return result;
874881
}
875882

@@ -3441,17 +3448,17 @@ int main(int argc, const char * argv[]) {
34413448
#if !CLOUDSYNC_DISABLE_ROWIDONLY_TABLES
34423449
table_mask |= TEST_NOPRIKEYS;
34433450
#endif
3444-
3451+
34453452
// test local changes
34463453
result += test_report("Local Test:", do_test_local(test_mask, table_mask, db, print_result));
34473454
result += test_report("VTab Test: ", do_test_vtab(db));
34483455
result += test_report("Functions Test:", do_test_functions(db, print_result));
34493456
result += test_report("Functions Test (Int):", do_test_internal_functions());
34503457
result += test_report("String Func Test:", do_test_string_replace_prefix());
3451-
3458+
34523459
// close local database
34533460
db = close_db(db);
3454-
3461+
34553462
// simulate remote merge
34563463
result += test_report("Merge Test:", do_test_merge(3, print_result, cleanup_databases));
34573464
result += test_report("Merge Test 2:", do_test_merge_2(3, TEST_PRIKEYS, print_result, cleanup_databases));
@@ -3477,5 +3484,13 @@ int main(int argc, const char * argv[]) {
34773484
if (rc != SQLITE_OK) printf("%s (%d)\n", (db) ? sqlite3_errmsg(db) : "N/A", rc);
34783485
db = close_db(db);
34793486

3487+
cloudsync_memory_finalize();
3488+
3489+
sqlite3_int64 memory_used = sqlite3_memory_used();
3490+
if (memory_used > 0) {
3491+
printf("Memory leaked: %lld B\n", memory_used);
3492+
result++;
3493+
}
3494+
34803495
return result;
34813496
}

0 commit comments

Comments
 (0)