GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — src: use `unique_ptr` for ffi memory management · nodejs/node@fda439c

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • node_ffi.cc
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)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.