Skip to content

Commit

Permalink
Fix reference leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager committed Sep 9, 2024
1 parent 5911c74 commit 77e8fc8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 36 deletions.
68 changes: 38 additions & 30 deletions binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,18 @@ rocksdb_native__on_iterator_close (rocksdb_iterator_t *handle, int status) {

err = js_close_handle_scope(env, scope);
assert(err == 0);

err = js_delete_reference(env, iterator->on_open);
assert(err == 0);

err = js_delete_reference(env, iterator->on_close);
assert(err == 0);

err = js_delete_reference(env, iterator->on_read);
assert(err == 0);

err = js_delete_reference(env, iterator->ctx);
assert(err == 0);
}

static js_value_t *
Expand Down Expand Up @@ -620,14 +632,6 @@ static js_value_t *
rocksdb_native_read_init (js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 1;
js_value_t *argv[1];

err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
assert(err == 0);

assert(argc == 1);

js_value_t *handle;

rocksdb_native_read_batch_t *batch;
Expand All @@ -637,9 +641,6 @@ rocksdb_native_read_init (js_env_t *env, js_callback_info_t *info) {
batch->env = env;
batch->handle.data = (void *) batch;

err = js_create_reference(env, argv[0], 1, &batch->ctx);
assert(err == 0);

return handle;
}

Expand Down Expand Up @@ -740,19 +741,25 @@ rocksdb_native__on_read (rocksdb_read_batch_t *handle, int status) {

err = js_close_handle_scope(env, scope);
assert(err == 0);

err = js_delete_reference(env, batch->on_status);
assert(err == 0);

err = js_delete_reference(env, batch->ctx);
assert(err == 0);
}

static js_value_t *
rocksdb_native_read (js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 5;
js_value_t *argv[5];
size_t argc = 6;
js_value_t *argv[6];

err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
assert(err == 0);

assert(argc == 5);
assert(argc == 6);

rocksdb_native_t *db;
err = js_get_arraybuffer_info(env, argv[0], (void **) &db, NULL);
Expand All @@ -766,7 +773,10 @@ rocksdb_native_read (js_env_t *env, js_callback_info_t *info) {
err = js_get_array_length(env, argv[2], &len);
assert(err == 0);

err = js_create_reference(env, argv[4], 1, &batch->on_status);
err = js_create_reference(env, argv[4], 0, &batch->ctx);
assert(err == 0);

err = js_create_reference(env, argv[5], 0, &batch->on_status);
assert(err == 0);

for (uint32_t i = 0; i < len; i++) {
Expand Down Expand Up @@ -821,14 +831,6 @@ static js_value_t *
rocksdb_native_write_init (js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 1;
js_value_t *argv[1];

err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
assert(err == 0);

assert(argc == 1);

js_value_t *handle;

rocksdb_native_write_batch_t *batch;
Expand All @@ -838,9 +840,6 @@ rocksdb_native_write_init (js_env_t *env, js_callback_info_t *info) {
batch->env = env;
batch->handle.data = (void *) batch;

err = js_create_reference(env, argv[0], 1, &batch->ctx);
assert(err == 0);

return handle;
}

Expand Down Expand Up @@ -919,19 +918,25 @@ rocksdb_native__on_write (rocksdb_write_batch_t *handle, int status) {

err = js_close_handle_scope(env, scope);
assert(err == 0);

err = js_delete_reference(env, batch->on_status);
assert(err == 0);

err = js_delete_reference(env, batch->ctx);
assert(err == 0);
}

static js_value_t *
rocksdb_native_write (js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 4;
js_value_t *argv[4];
size_t argc = 5;
js_value_t *argv[5];

err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
assert(err == 0);

assert(argc == 4);
assert(argc == 5);

rocksdb_native_t *db;
err = js_get_arraybuffer_info(env, argv[0], (void **) &db, NULL);
Expand All @@ -945,7 +950,10 @@ rocksdb_native_write (js_env_t *env, js_callback_info_t *info) {
err = js_get_array_length(env, argv[2], &len);
assert(err == 0);

err = js_create_reference(env, argv[3], 1, &batch->on_status);
err = js_create_reference(env, argv[3], 1, &batch->ctx);
assert(err == 0);

err = js_create_reference(env, argv[4], 1, &batch->on_status);
assert(err == 0);

for (uint32_t i = 0; i < len; i++) {
Expand Down
10 changes: 4 additions & 6 deletions lib/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class RocksDBBatch {
this._request = null
this._resolveRequest = null

this._destroying = null

this._handle = null
this._buffer = null

Expand Down Expand Up @@ -118,7 +116,7 @@ exports.ReadBatch = class RocksDBReadBatch extends RocksDBBatch {
}

_init () {
this._handle = binding.readInit(this)
this._handle = binding.readInit()
this._buffer = binding.readBuffer(this._handle, this._capacity)
}

Expand All @@ -131,7 +129,7 @@ exports.ReadBatch = class RocksDBReadBatch extends RocksDBBatch {
async _flush () {
await super._flush()

binding.read(this._db._handle, this._handle, this._operations, this._snapshot ? this._snapshot._handle : null, this._onread)
binding.read(this._db._handle, this._handle, this._operations, this._snapshot ? this._snapshot._handle : null, this, this._onread)
}

_onread (errs, values) {
Expand Down Expand Up @@ -162,7 +160,7 @@ exports.ReadBatch = class RocksDBReadBatch extends RocksDBBatch {

exports.WriteBatch = class RocksDBWriteBatch extends RocksDBBatch {
_init () {
this._handle = binding.writeInit(this)
this._handle = binding.writeInit()
this._buffer = binding.writeBuffer(this._handle, this._capacity)
}

Expand All @@ -175,7 +173,7 @@ exports.WriteBatch = class RocksDBWriteBatch extends RocksDBBatch {
async _flush () {
await super._flush()

binding.write(this._db._handle, this._handle, this._operations, this._onwrite)
binding.write(this._db._handle, this._handle, this._operations, this, this._onwrite)
}

_onwrite (err) {
Expand Down

0 comments on commit 77e8fc8

Please sign in to comment.