diff --git a/napi-inl.h b/napi-inl.h index b831f34c9..13fea5b91 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -1657,7 +1657,11 @@ inline Function Function::New(napi_env env, CbData::Wrapper, callbackData, &value); - NAPI_THROW_IF_FAILED(env, status, Function()); + if (status != napi_ok) { + delete callbackData; + NAPI_THROW_IF_FAILED(env, status, Function()); + } + return Function(env, value); } @@ -2608,8 +2612,11 @@ PropertyDescriptor::Accessor(Napi::Env env, auto callbackData = new CbData({ getter, nullptr }); napi_status status = AttachData(env, object, callbackData); - NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor()); - + if (status != napi_ok) { + delete callbackData; + NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor()); + } + return PropertyDescriptor({ utf8name, nullptr, @@ -2643,7 +2650,10 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(Napi::Env env, auto callbackData = new CbData({ getter, nullptr }); napi_status status = AttachData(env, object, callbackData); - NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor()); + if (status != napi_ok) { + delete callbackData; + NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor()); + } return PropertyDescriptor({ nullptr, @@ -2669,7 +2679,10 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(Napi::Env env, auto callbackData = new CbData({ getter, setter }); napi_status status = AttachData(env, object, callbackData); - NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor()); + if (status != napi_ok) { + delete callbackData; + NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor()); + } return PropertyDescriptor({ utf8name, @@ -2706,7 +2719,10 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(Napi::Env env, auto callbackData = new CbData({ getter, setter }); napi_status status = AttachData(env, object, callbackData); - NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor()); + if (status != napi_ok) { + delete callbackData; + NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor()); + } return PropertyDescriptor({ nullptr,