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

Conversation

@PatriceJiang
Copy link
Contributor

@PatriceJiang PatriceJiang commented May 22, 2019

ref https://forum.cocos.com/t/ios/76826, https://forum.cocos.com/t/ios-glteximage2d-exc-bad-access/74364

防止对象被提前GC

@PatriceJiang
Copy link
Contributor Author

@PatriceJiang PatriceJiang commented May 22, 2019

@minggo 麻烦有空review下

@@ -436,13 +436,15 @@ static bool js_CanvasRenderingContext2D_setCanvasBufferUpdatedCallback(se::State
se::ValueArray args;
args.resize(1);
ok &= Data_to_seval(larg0, &args[0]);
args[0].toObject()->root();
Copy link
Contributor

@minggo minggo May 22, 2019

se::Value 的构造函数可以传入参数防止 GC

Copy link
Contributor Author

@PatriceJiang PatriceJiang May 22, 2019

改成在Data_to_seval中处理

@@ -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

@minggo minggo May 22, 2019

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

Copy link
Contributor Author

@PatriceJiang PatriceJiang May 22, 2019

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

Copy link
Contributor

@minggo minggo May 22, 2019

有两个疑问:

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

Copy link
Contributor Author

@PatriceJiang PatriceJiang May 23, 2019

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

@minggo
Copy link
Contributor

@minggo minggo commented May 23, 2019

GC 的问题先这么临时处理吧。等使用了 V8 有了 scope 后就不会有这个问题了。不过 JSC 也是很奇葩,新创建的内存照理应该是放在新生代,怎么这么快就被 GC 了。

@jareguo
Copy link
Member

@jareguo jareguo commented May 23, 2019

这个问题也是这个地方引起的吗? https://forum.cocos.com/t/jsobjectref-root-unroot-gc/74369

@PatriceJiang
Copy link
Contributor Author

@PatriceJiang PatriceJiang commented May 23, 2019

是的 @jareguo

minggo
minggo approved these changes May 23, 2019
@jareguo jareguo changed the base branch from develop to v2.0.10-release May 23, 2019
@jareguo jareguo merged commit bba5ed2 into cocos-creator:v2.0.10-release May 23, 2019
1 check passed
@jareguo
Copy link
Member

@jareguo jareguo commented May 23, 2019

修复原生平台上 GC 可能导致崩溃的问题

@caochao
Copy link

@caochao caochao commented May 27, 2019

赞一个

@PatriceJiang PatriceJiang deleted the root_typearrary branch Oct 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants