From 77e8fc80f2c99c13455bd33b9ff52e4aa281bfce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Mon, 9 Sep 2024 11:26:35 +0200 Subject: [PATCH] Fix reference leaks --- binding.c | 68 +++++++++++++++++++++++++++++----------------------- lib/batch.js | 10 ++++---- 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/binding.c b/binding.c index 92fe1e3..c65cf74 100644 --- a/binding.c +++ b/binding.c @@ -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 * @@ -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; @@ -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; } @@ -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); @@ -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++) { @@ -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; @@ -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; } @@ -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); @@ -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++) { diff --git a/lib/batch.js b/lib/batch.js index ca58f70..a6e8f55 100644 --- a/lib/batch.js +++ b/lib/batch.js @@ -26,8 +26,6 @@ class RocksDBBatch { this._request = null this._resolveRequest = null - this._destroying = null - this._handle = null this._buffer = null @@ -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) } @@ -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) { @@ -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) } @@ -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) {