You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
"Class property and descriptor" example crashes in Electron render process on reload #1011
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.
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.]
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?
@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.
Activity
KevinEady commentedon Jun 16, 2021
Hi @ToadKing ,
Thanks for pointing this out. The example in the documentation predates some modern APIs for handling context-sensitivity, particularly
SetInstanceDataandGetInstanceData. 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 commentedon Jun 16, 2021
Note that
SetInstanceDatais only available on Node-API 6+, see Node-API version matrix.ToadKing commentedon Jun 16, 2021
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 commentedon Jun 16, 2021
@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
doc: update examples for context sensitivity
doc: update examples for context sensitivity
doc: update examples for context sensitivity
doc: update examples for context sensitivity
doc: update examples for context sensitivity
doc: update examples for context sensitivity
doc: update examples for context sensitivity
doc: update examples for context sensitivity