Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Class property and descriptor" example crashes in Electron render process on reload #1011

Closed
ToadKing opened this issue Jun 16, 2021 · 4 comments
Assignees

Comments

@ToadKing
Copy link

ToadKing commented Jun 16, 2021

PoC Project: https://github.com/ToadKing/electron-native-module-poc (The native module part is an exact copy of the example in the node-addon-api docs.)

The class example in the docs makes the constructor reference persistent using Napi::Persistent on module initialization. However, in Electron when a render process reloads a page, the native module is re-initialized without reloading the shared library itself. When a new constructor reference is loaded into the constructor variable it tries to delete the former reference, which causes a crash of the Electron render process.

@KevinEady
Copy link
Contributor

KevinEady commented Jun 16, 2021

Hi @ToadKing ,

Thanks for pointing this out. The example in the documentation predates some modern APIs for handling context-sensitivity, particularly SetInstanceData and GetInstanceData. You can update https://github.com/ToadKing/electron-native-module-poc/blob/76d02d8c7cb151f3a657734836012629fc2f4f8c/native/src/binding.cc#L23-L24 with:

    Napi::FunctionReference *constructor = new Napi::FunctionReference();
    *constructor = Napi::Persistent(func);
    env.SetInstanceData(constructor);
electron-reload.mp4

[NB: SetInstanceData will overwrite any existing instance data set. See #744 for some examples on how to store multiple constructors on the instance data.]

@KevinEady
Copy link
Contributor

Note that SetInstanceData is only available on Node-API 6+, see Node-API version matrix.

@ToadKing
Copy link
Author

Thank you, this change fixes my PoC app and the app I found it in. Should this issue be left open and be changed to update the documentation or is it good to close?

@KevinEady
Copy link
Contributor

@ToadKing I am not sure... We will leave it open to discuss at this week's Node-API meeting. We'll need to discuss a hopefully context-sensitive approach for Node-API version <= 5. I will leave this issue open until we discuss it.

Thanks, Kevin

@KevinEady KevinEady self-assigned this Jun 16, 2021
KevinEady added a commit to KevinEady/node-addon-api that referenced this issue Jun 21, 2021
KevinEady added a commit to KevinEady/node-addon-api that referenced this issue Jun 21, 2021
deepakrkris pushed a commit to deepakrkris/node-addon-api that referenced this issue Sep 23, 2021
Fixes: nodejs#1011

PR-URL: nodejs#1013
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
deepakrkris pushed a commit to deepakrkris/node-addon-api that referenced this issue Oct 15, 2021
Fixes: nodejs#1011

PR-URL: nodejs#1013
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
kevindavies8 added a commit to kevindavies8/node-addon-api-Develop that referenced this issue Aug 24, 2022
Fixes: nodejs/node-addon-api#1011

PR-URL: nodejs/node-addon-api#1013
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Marlyfleitas added a commit to Marlyfleitas/node-api-addon-Development that referenced this issue Aug 26, 2022
Fixes: nodejs/node-addon-api#1011

PR-URL: nodejs/node-addon-api#1013
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
wroy7860 added a commit to wroy7860/addon-api-benchmark-node that referenced this issue Sep 19, 2022
Fixes: nodejs/node-addon-api#1011

PR-URL: nodejs/node-addon-api#1013
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
johnfrench3 pushed a commit to johnfrench3/node-addon-api-git that referenced this issue Aug 11, 2023
Fixes: nodejs/node-addon-api#1011

PR-URL: nodejs/node-addon-api#1013
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants