Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fda439c

Browse filesBrowse 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
Copy full SHA for fda439c

1 file changed

+13-16Lines changed: 13 additions & 16 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/node_ffi.cc‎

Copy file name to clipboardExpand all lines: src/node_ffi.cc
+13-16Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.