Skip to content

Commit

Permalink
src: keep reference buffer alive longer
Browse files Browse the repository at this point in the history
Keep a buffer written by WritePointer alive
until the finalizers for the buffer to which
the pointer has been run have been executed:

Refs: #54

Signed-off-by: Michael Dawson <mdawson@devrus.com>
  • Loading branch information
mhdawson committed Feb 12, 2021
1 parent b0809c2 commit c882e1c
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/binding.cc
Expand Up @@ -355,6 +355,16 @@ void WritePointer(const CallbackInfo& args) {
if (input.IsNull()) {
*reinterpret_cast<char**>(ptr) = nullptr;
} else {
// create a node-api reference and finalizer to ensure that
// the buffer whoes pointer is written can only be
// collected after the finalizers for the buffer
// to which the pointer was written have already run
Reference<Value>* ref = new Reference<Value>;
*ref = Persistent(args[2]);
args[0].As<Object>().AddFinalizer([](Env env, Reference<Value>* ref) {
delete ref;
}, ref);

char* input_ptr = GetBufferData(input);
*reinterpret_cast<char**>(ptr) = input_ptr;
}
Expand Down

0 comments on commit c882e1c

Please sign in to comment.