From 029ad07c819511ad9293bd956638d1e2b94ffdc4 Mon Sep 17 00:00:00 2001 From: Fritz Mueller Date: Tue, 10 Dec 2019 16:09:10 -0800 Subject: [PATCH] Fix crashes after throw from ctor of Napi::ObjectWrap subclasses See nodejs/node-addon-api#475 --- src/CGALWrapper-inl.h | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/CGALWrapper-inl.h b/src/CGALWrapper-inl.h index 74e610e..d7f07df 100644 --- a/src/CGALWrapper-inl.h +++ b/src/CGALWrapper-inl.h @@ -19,9 +19,25 @@ CGALWrapper::CGALWrapper(Napi::CallbackInfo const& info : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - ARGS_ASSERT(env, info.Length() <= 1); - if (info.Length() == 1) { - ARGS_ASSERT(env, WrapperClass::ParseArg(env, info[0], mWrapped)); + + // This try/catch/rethrow is a workaround for a bug in Napi::ObjectWrap<> destruction; + // see https://github.com/nodejs/node-addon-api/pull/475 + + try { + ARGS_ASSERT(env, info.Length() <= 1); + if (info.Length() == 1) { + ARGS_ASSERT(env, WrapperClass::ParseArg(env, info[0], mWrapped)); + } + } + + catch(std::exception const&) { + if (!CGALWrapper::IsEmpty()) { + Napi::Object object = CGALWrapper::Value(); + if (!object.IsEmpty()) { + napi_remove_wrap(env, object, nullptr); + } + } + throw; } }