forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
n-api: free instance data as reference
Instance data associated with a `napi_env` is no longer stored on the env itself but is instead rendered as a reference. Since `v8impl::Reference` is tied to a JS object, this modification factors out the `v8impl::Reference` refcounting and the deletion process into a base class for `v8impl::Reference`, called `v8impl::RefBase`. The instance data is then stored as a `v8impl::RefBase`, along with other references, preventing a segfault that arises from the fact that, up until now, upon `napi_env` destruction, the instance data was freed after all references had already been forcefully freed. If the addon freed a reference during the `napi_set_instance_data` finalizer callback, such a reference had already been freed during environment teardown, causing a double free. Re: nodejs/node-addon-api#663 PR-URL: nodejs#31638 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: David Carlier <devnexen@gmail.com>
- Loading branch information
Gabriel Schulhof
committed
Feb 6, 2020
1 parent
a9e2626
commit 884e287
Showing
8 changed files
with
232 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#define NAPI_EXPERIMENTAL | ||
#include <node_api.h> | ||
|
||
static void addon_free(napi_env env, void* data, void* hint) { | ||
napi_ref* ref = data; | ||
napi_delete_reference(env, *ref); | ||
free(ref); | ||
fprintf(stderr, "addon_free"); | ||
} | ||
|
||
napi_value addon_new(napi_env env, napi_value exports, bool ref_first) { | ||
napi_ref* ref = malloc(sizeof(*ref)); | ||
if (ref_first) { | ||
napi_create_reference(env, exports, 1, ref); | ||
napi_set_instance_data(env, ref, addon_free, NULL); | ||
} else { | ||
napi_set_instance_data(env, ref, addon_free, NULL); | ||
napi_create_reference(env, exports, 1, ref); | ||
} | ||
return exports; | ||
} |
Oops, something went wrong.