From 5e016439c0d64f9605d69da5e64d0dc4b6dce099 Mon Sep 17 00:00:00 2001 From: Mike Kaufman Date: Fri, 18 Aug 2017 12:19:21 -0700 Subject: [PATCH] chakrashim: ref count data sent to SetEmbedderData JS Objects passed to SetEmbedderData need to have JsAddRef/JsRelease called, otherwise they could be GC'd prematurely. Fixes: https://github.com/nodejs/node-chakracore/issues/97 PR-URL: https://github.com/nodejs/node-chakracore/pull/374 Reviewed-By: Kyle Farnung Reviewed-By: Hitesh Kanwathirtha Reviewed-By: Kunal Pathak Reviewed-By: Jimmy Thomson --- deps/chakrashim/src/jsrtcontextshim.cc | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/deps/chakrashim/src/jsrtcontextshim.cc b/deps/chakrashim/src/jsrtcontextshim.cc index e602e27024d..15aefc3809c 100644 --- a/deps/chakrashim/src/jsrtcontextshim.cc +++ b/deps/chakrashim/src/jsrtcontextshim.cc @@ -471,6 +471,15 @@ void ContextShim::SetAlignedPointerInEmbedderData(int index, void * value) { if (embedderData.size() < minSize) { embedderData.resize(minSize); } + + // ensure reference counting, otherwise objects can be GC'd. JsAddRef/ + // JsRelease will handle cases if the pointer is not valid for ref counts. + void * oldValue = embedderData[index]; + if (oldValue != nullptr) { + JsRelease(oldValue, nullptr); + } + JsAddRef(value, nullptr); + embedderData[index] = value; } catch(const std::exception&) { }