Skip to content

Commit 0eb6042

Browse files
committed
src: use unique_ptr for ffi memory management
Signed-off-by: Anna Henningsen <anna@addaleax.net>
1 parent 69a970f commit 0eb6042

1 file changed

Lines changed: 13 additions & 16 deletions

File tree

src/node_ffi.cc

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -874,22 +874,22 @@ void DynamicLibrary::RegisterCallback(const FunctionCallbackInfo<Value>& args) {
874874
return;
875875
}
876876

877-
auto callback = new FFICallback{.owner = lib,
878-
.env = env,
879-
.thread_id = std::this_thread::get_id(),
880-
.fn = Global<Function>(isolate, fn),
881-
.closure = nullptr,
882-
.ptr = nullptr,
883-
.cif = {},
884-
.args = std::move(callback_args),
885-
.return_type = return_type};
877+
auto callback = std::unique_ptr<FFICallback>(
878+
new FFICallback{.owner = lib,
879+
.env = env,
880+
.thread_id = std::this_thread::get_id(),
881+
.fn = Global<Function>(isolate, fn),
882+
.closure = nullptr,
883+
.ptr = nullptr,
884+
.cif = {},
885+
.args = std::move(callback_args),
886+
.return_type = return_type});
886887

887888
callback->closure = static_cast<ffi_closure*>(
888889
ffi_closure_alloc(sizeof(ffi_closure), &callback->ptr));
889890

890891
if (callback->closure == nullptr) {
891892
THROW_ERR_FFI_CALL_FAILED(env, "ffi_closure_alloc failed");
892-
delete callback;
893893
return;
894894
}
895895

@@ -914,7 +914,6 @@ void DynamicLibrary::RegisterCallback(const FunctionCallbackInfo<Value>& args) {
914914
}
915915

916916
THROW_ERR_FFI_CALL_FAILED(env, msg);
917-
delete callback;
918917
return;
919918
}
920919

@@ -938,14 +937,12 @@ void DynamicLibrary::RegisterCallback(const FunctionCallbackInfo<Value>& args) {
938937
}
939938

940939
THROW_ERR_FFI_CALL_FAILED(env, msg);
941-
delete callback;
942940
return;
943941
}
944942

945-
lib->callbacks_.emplace(callback->ptr, callback);
946-
args.GetReturnValue().Set(BigInt::NewFromUnsigned(
947-
isolate,
948-
static_cast<uint64_t>(reinterpret_cast<uintptr_t>(callback->ptr))));
943+
auto ret = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(callback->ptr));
944+
lib->callbacks_.emplace(callback->ptr, std::move(callback));
945+
args.GetReturnValue().Set(BigInt::NewFromUnsigned(isolate, ret));
949946
}
950947

951948
void DynamicLibrary::UnregisterCallback(

0 commit comments

Comments
 (0)