-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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: #31638 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: David Carlier <[email protected]>
- Loading branch information
1 parent
8f75c74
commit 08e09ec
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.