diff --git a/native/cocos/scripting/js-bindings/docs/JSB2.0-learning-en.md b/native/cocos/scripting/js-bindings/docs/JSB2.0-learning-en.md index 6b220e2221..15d360a8ac 100644 --- a/native/cocos/scripting/js-bindings/docs/JSB2.0-learning-en.md +++ b/native/cocos/scripting/js-bindings/docs/JSB2.0-learning-en.md @@ -205,8 +205,10 @@ spTrackEntry_setDisposeCallback([](spTrackEntry* entry){ se::AutoHandleScope hs; se->clearException(); - // The native <-> JS mapping was cleared in the callback above. - // seObj->clearPrivateData isn't needed since the JS object will be garbage collected after unroot and decRef. + // The mapping of native object & se::Object was cleared in above code. + // The private data (native object) may be a different object associated with other se::Object. + // Therefore, don't clear the mapping again. + seObj->clearPrivateData(false); seObj->unroot(); seObj->decRef(); }; diff --git a/native/cocos/scripting/js-bindings/docs/JSB2.0-learning-zh.md b/native/cocos/scripting/js-bindings/docs/JSB2.0-learning-zh.md index a1f16a1aa1..68713522f7 100644 --- a/native/cocos/scripting/js-bindings/docs/JSB2.0-learning-zh.md +++ b/native/cocos/scripting/js-bindings/docs/JSB2.0-learning-zh.md @@ -204,10 +204,9 @@ spTrackEntry_setDisposeCallback([](spTrackEntry* entry){ se::AutoHandleScope hs; se->clearException(); - // 没必要调用 seObj->clearPrivateData ,因为 Native 对象与 JS 对象的引用关系已经在上面被解除。 - // 如果调用,反而会导致把新生成的 Native 对象的引用关系错误解除掉。 - // 设置 seObj 的 privateData 为 nullptr 也无意义,因为在执行 unroot 和 decRef 后, - // 当前 JS 对象即将被回收。 + // 由于上面逻辑已经把映射关系解除了,这里传入 false 表示不用再次解除映射关系, + // 因为当前 seObj 的 private data 可能已经是另外一个不同的对象 + seObj->clearPrivateData(false); seObj->unroot(); // unroot,使 JS 对象受 GC 管理 seObj->decRef(); // 释放 se::Object }; diff --git a/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.cpp b/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.cpp index 7efe2dfa3e..26e94bddf0 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.cpp @@ -661,12 +661,12 @@ namespace se { _privateData = data; } - void Object::clearPrivateData() + void Object::clearPrivateData(bool clearMapping) { if (_privateData != nullptr) { - void* data = getPrivateData(); - NativePtrToObjectMap::erase(data); + if (clearMapping) + NativePtrToObjectMap::erase(_privateData); internal::clearPrivate(_obj); _privateData = nullptr; } diff --git a/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.hpp b/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.hpp index 4c747910e3..9a6aa64f84 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.hpp @@ -254,11 +254,10 @@ namespace se { void* getPrivateData() const; /** - * @brief Roots an object from garbage collection. - * @note Use this method when you want to store an object in a global or on the heap, where the garbage collector will not be able to discover your reference to it. - * An object may be rooted multiple times and must be unrooted an equal number of times before becoming eligible for garbage collection. + * @brief Clears private data of an object. + * @param clearMapping Whether to clear the mapping of native object & se::Object. */ - void clearPrivateData(); + void clearPrivateData(bool clearMapping = true); /** * @brief Roots an object from garbage collection. diff --git a/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.cpp b/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.cpp index 84bcb00573..f3424736c8 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.cpp @@ -1123,12 +1123,12 @@ namespace se { _privateData = data; } - void Object::clearPrivateData() + void Object::clearPrivateData(bool clearMapping) { if (_privateData != nullptr) { - void* data = getPrivateData(); - NativePtrToObjectMap::erase(data); + if (clearMapping) + NativePtrToObjectMap::erase(_privateData); internal::clearPrivate(_obj); _privateData = nullptr; } diff --git a/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.hpp b/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.hpp index 3c79b0f443..736606c911 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.hpp @@ -256,8 +256,9 @@ namespace se { /** * @brief Clears private data of an object. + * @param clearMapping Whether to clear the mapping of native object & se::Object. */ - void clearPrivateData(); + void clearPrivateData(bool clearMapping = true); /** * @brief Roots an object from garbage collection. diff --git a/native/cocos/scripting/js-bindings/jswrapper/sm/Object.cpp b/native/cocos/scripting/js-bindings/jswrapper/sm/Object.cpp index 5e1759015b..56be108331 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/sm/Object.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/sm/Object.cpp @@ -486,12 +486,12 @@ namespace se { _privateData = data; } - void Object::clearPrivateData() + void Object::clearPrivateData(bool clearMapping) { if (_privateData != nullptr) { - void* data = getPrivateData(); - NativePtrToObjectMap::erase(data); + if (clearMapping) + NativePtrToObjectMap::erase(_privateData); JS::RootedObject obj(__cx, _getJSObject()); internal::clearPrivate(__cx, obj); _privateData = nullptr; diff --git a/native/cocos/scripting/js-bindings/jswrapper/sm/Object.hpp b/native/cocos/scripting/js-bindings/jswrapper/sm/Object.hpp index 6edf7781a0..5141e76877 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/sm/Object.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/sm/Object.hpp @@ -256,8 +256,9 @@ namespace se { /** * @brief Clears private data of an object. + * @param clearMapping Whether to clear the mapping of native object & se::Object. */ - void clearPrivateData(); + void clearPrivateData(bool clearMapping = true); /** * @brief Roots an object from garbage collection. diff --git a/native/cocos/scripting/js-bindings/jswrapper/v8/Object.cpp b/native/cocos/scripting/js-bindings/jswrapper/v8/Object.cpp index 9bad7be321..f08d911dad 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/v8/Object.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/v8/Object.cpp @@ -450,12 +450,12 @@ namespace se { return _privateData; } - void Object::clearPrivateData() + void Object::clearPrivateData(bool clearMapping) { if (_privateData != nullptr) { - void* data = getPrivateData(); - NativePtrToObjectMap::erase(data); + if (clearMapping) + NativePtrToObjectMap::erase(_privateData); internal::clearPrivate(__isolate, _obj); _privateData = nullptr; } diff --git a/native/cocos/scripting/js-bindings/jswrapper/v8/Object.hpp b/native/cocos/scripting/js-bindings/jswrapper/v8/Object.hpp index 1d397461bb..a86f91c30c 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/v8/Object.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/v8/Object.hpp @@ -261,8 +261,9 @@ namespace se { /** * @brief Clears private data of an object. + * @param clearMapping Whether to clear the mapping of native object & se::Object. */ - void clearPrivateData(); + void clearPrivateData(bool clearMapping = true); /** * @brief Roots an object from garbage collection. diff --git a/native/cocos/scripting/js-bindings/manual/jsb_box2d_manual.cpp b/native/cocos/scripting/js-bindings/manual/jsb_box2d_manual.cpp index ffc5ea121a..b415d0a84e 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_box2d_manual.cpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_box2d_manual.cpp @@ -1111,8 +1111,10 @@ bool register_all_box2d_manual(se::Object* obj) se::AutoHandleScope hs; se->clearException(); - // The native <-> JS mapping was cleared in the callback above. - // seObj->clearPrivateData isn't needed since the JS object will be garbage collected after unroot and decRef. + // The mapping of native object & se::Object was cleared in above code. + // The private data (native object) may be a different object associated with other se::Object. + // Therefore, don't clear the mapping again. + seObj->clearPrivateData(false); seObj->unroot(); seObj->decRef(); }; diff --git a/native/cocos/scripting/js-bindings/manual/jsb_conversions.hpp b/native/cocos/scripting/js-bindings/manual/jsb_conversions.hpp index a46e85e970..883d975edd 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_conversions.hpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_conversions.hpp @@ -239,32 +239,6 @@ bool AffineTransform_to_seval(const cocos2d::AffineTransform& v, se::Value* ret) bool Data_to_seval(const cocos2d::Data& v, se::Value* ret); bool DownloadTask_to_seval(const cocos2d::network::DownloadTask& v, se::Value* ret); -template -bool recreate_seval_by_native_ptr(typename std::enable_if::value,T>::type* v, se::Class* cls, se::Value* ret) -{ - assert(ret != nullptr); - assert(cls != nullptr); - if (v == nullptr) - { - ret->setNull(); - return true; - } - - auto iter = se::NativePtrToObjectMap::find(v); - if (iter != se::NativePtrToObjectMap::end()) - { - se::Object* seObj = iter->second; - seObj->clearPrivateData(); - seObj->decRef(); - } - - se::Object* obj = se::Object::createObjectWithClass(cls); - ret->setObject(obj, true); - obj->setPrivateData(v); - - return true; -} - template bool native_ptr_to_seval(typename std::enable_if::value,T>::type* v, se::Value* ret, bool* isReturnCachedValue = nullptr) { diff --git a/native/cocos/scripting/js-bindings/manual/jsb_dragonbones_manual.cpp b/native/cocos/scripting/js-bindings/manual/jsb_dragonbones_manual.cpp index 142732d1e0..b417399b85 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_dragonbones_manual.cpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_dragonbones_manual.cpp @@ -449,8 +449,10 @@ bool register_all_dragonbones_manual(se::Object* obj) se::AutoHandleScope hs; se->clearException(); - // The native <-> JS mapping was cleared in the callback above. - // seObj->clearPrivateData isn't needed since the JS object will be garbage collected after unroot and decRef. + // The mapping of native object & se::Object was cleared in above code. + // The private data (native object) may be a different object associated with other se::Object. + // Therefore, don't clear the mapping again. + seObj->clearPrivateData(false); seObj->unroot(); seObj->decRef(); }; diff --git a/native/cocos/scripting/js-bindings/manual/jsb_spine_manual.cpp b/native/cocos/scripting/js-bindings/manual/jsb_spine_manual.cpp index df5c4a48b2..48afa846c6 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_spine_manual.cpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_spine_manual.cpp @@ -306,8 +306,10 @@ static bool js_register_spine_TrackEntry(se::Object* obj) se::AutoHandleScope hs; se->clearException(); - // The native <-> JS mapping was cleared in the callback above. - // seObj->clearPrivateData isn't needed since the JS object will be garbage collected after unroot and decRef. + // The mapping of native object & se::Object was cleared in above code. + // The private data (native object) may be a different object associated with other se::Object. + // Therefore, don't clear the mapping again. + seObj->clearPrivateData(false); seObj->unroot(); seObj->decRef(); };