Skip to content

Commit fda439c

Browse files
addaleaxaduh95
authored andcommitted
src: use unique_ptr for ffi memory management
Signed-off-by: Anna Henningsen <anna@addaleax.net> PR-URL: #63071 Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent b10653a commit fda439c

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
@@ -714,22 +714,22 @@ void DynamicLibrary::RegisterCallback(const FunctionCallbackInfo<Value>& args) {
714714
return;
715715
}
716716

717-
auto callback = new FFICallback{.owner = lib,
718-
.env = env,
719-
.thread_id = std::this_thread::get_id(),
720-
.fn = Global<Function>(isolate, fn),
721-
.closure = nullptr,
722-
.ptr = nullptr,
723-
.cif = {},
724-
.args = std::move(callback_args),
725-
.return_type = return_type};
717+
auto callback = std::unique_ptr<FFICallback>(
718+
new FFICallback{.owner = lib,
719+
.env = env,
720+
.thread_id = std::this_thread::get_id(),
721+
.fn = Global<Function>(isolate, fn),
722+
.closure = nullptr,
723+
.ptr = nullptr,
724+
.cif = {},
725+
.args = std::move(callback_args),
726+
.return_type = return_type});
726727

727728
callback->closure = static_cast<ffi_closure*>(
728729
ffi_closure_alloc(sizeof(ffi_closure), &callback->ptr));
729730

730731
if (callback->closure == nullptr) {
731732
THROW_ERR_FFI_CALL_FAILED(env, "ffi_closure_alloc failed");
732-
delete callback;
733733
return;
734734
}
735735

@@ -754,7 +754,6 @@ void DynamicLibrary::RegisterCallback(const FunctionCallbackInfo<Value>& args) {
754754
}
755755

756756
THROW_ERR_FFI_CALL_FAILED(env, msg);
757-
delete callback;
758757
return;
759758
}
760759

@@ -778,14 +777,12 @@ void DynamicLibrary::RegisterCallback(const FunctionCallbackInfo<Value>& args) {
778777
}
779778

780779
THROW_ERR_FFI_CALL_FAILED(env, msg);
781-
delete callback;
782780
return;
783781
}
784782

785-
lib->callbacks_.emplace(callback->ptr, callback);
786-
args.GetReturnValue().Set(BigInt::NewFromUnsigned(
787-
isolate,
788-
static_cast<uint64_t>(reinterpret_cast<uintptr_t>(callback->ptr))));
783+
auto ret = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(callback->ptr));
784+
lib->callbacks_.emplace(callback->ptr, std::move(callback));
785+
args.GetReturnValue().Set(BigInt::NewFromUnsigned(isolate, ret));
789786
}
790787

791788
void DynamicLibrary::UnregisterCallback(

0 commit comments

Comments
 (0)