mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
Fix potential issues in the mode of cpp controls js object's lifecycle. (#1264)
* Fix potential issues in the mode of cpp controls js object's lifecycle. * Updates comments when using clearPrivateData(false);
This commit is contained in:
@@ -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();
|
||||
};
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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();
|
||||
};
|
||||
|
||||
@@ -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<typename T>
|
||||
bool recreate_seval_by_native_ptr(typename std::enable_if<!std::is_base_of<cocos2d::Ref,T>::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<typename T>
|
||||
bool native_ptr_to_seval(typename std::enable_if<!std::is_base_of<cocos2d::Ref,T>::value,T>::type* v, se::Value* ret, bool* isReturnCachedValue = nullptr)
|
||||
{
|
||||
|
||||
@@ -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();
|
||||
};
|
||||
|
||||
@@ -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();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user