Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protect native allocated typed array from being GC #1744

Merged
merged 3 commits into from May 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cocos/scripting/js-bindings/manual/jsb_conversions.cpp
Expand Up @@ -2509,7 +2509,7 @@ bool Data_to_seval(const cocos2d::Data& v, se::Value* ret)
else
{
se::HandleObject obj(se::Object::createTypedArray(se::Object::TypedArrayType::UINT8, v.getBytes(), v.getSize()));
ret->setObject(obj);
ret->setObject(obj, true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HandleObject 应该已经处理了 root, unroot 了吧。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HandleObject在析构时进行了unroot, 过早了。 需要把typed array扩展到se::Value* ret的生命周期。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有两个疑问:

  1. 这个文件里的所有函数都有这个问题。
  2. 如果这样的话,那么就没必要创建 HandleObject 对象的吧。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 是的
  2. HandleObject设计上可以 保护初始化过程中不被 GC

}
return true;
}
Expand Down
3 changes: 1 addition & 2 deletions cocos/scripting/js-bindings/manual/jsb_global.cpp
Expand Up @@ -849,13 +849,12 @@ bool jsb_global_load_image(const std::string& path, const se::Value& callbackVal
Application::getInstance()->getScheduler()->performFunctionInCocosThread([=](){
se::AutoHandleScope hs;
se::ValueArray seArgs;

se::Value dataVal;
if (loadSucceed)
{
se::HandleObject retObj(se::Object::createPlainObject());
Data data;
data.copy(imgInfo->data, imgInfo->length);
se::Value dataVal;
Data_to_seval(data, &dataVal);
retObj->setProperty("data", dataVal);
retObj->setProperty("width", se::Value(imgInfo->width));
Expand Down