As a result the originally created JSReference is collected by .Net GC and destroys the wrapper napi_ref stored as a private property in the JS object. When try to unwrap it later we observe a crash because the underlying napi_ref is deleted.
To work around this issue this PR adds a condition after calling createWrapper() to check if the _objectMap already has an entry and do nothing in such case.
Fixes: #296
When we create a new object wrapper in the
JsRuntimeContext.InitializeObjectWrapper
we add a reference to it to the_objectMap
: https://github.com/microsoft/node-api-dotnet/blob/25b3b6c9452a855a7062e9c58160afd2e276ad36/src/NodeApi/Interop/JSRuntimeContext.cs#L328Then, the calling method in the
JsRuntimeContext.GetOrCreateObjectWrapper
creates anotherJSReference
to the same wrapper and replaces it in the_objectMap
: https://github.com/microsoft/node-api-dotnet/blob/25b3b6c9452a855a7062e9c58160afd2e276ad36/src/NodeApi/Interop/JSRuntimeContext.cs#L398As a result the originally created
JSReference
is collected by .Net GC and destroys the wrappernapi_ref
stored as a private property in the JS object. When try to unwrap it later we observe a crash because the underlyingnapi_ref
is deleted.To work around this issue this PR adds a condition after calling
createWrapper()
to check if the_objectMap
already has an entry and do nothing in such case.