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

Fixed memory leak caused by HtmlCanvasElement not being released. #1786

Merged
merged 3 commits into from Aug 2, 2019

Conversation

@caryliu1999
Copy link
Contributor

commented Jul 31, 2019

@@ -439,6 +439,8 @@ static bool js_CanvasRenderingContext2D_setCanvasBufferUpdatedCallback(se::State
se::Value rval;
se::Object* thisObj = jsThis.isObject() ? jsThis.toObject() : nullptr;
se::Object* funcObj = jsFunc.toObject();
thisObj->unroot();
funcObj->unroot();

This comment has been minimized.

Copy link
@minggo

minggo Jul 31, 2019

Contributor

之前是哪里进行了 root?另外,thisObj 的值可能为 nullptr。

This comment has been minimized.

Copy link
@caryliu1999

caryliu1999 Jul 31, 2019

Author Contributor

修改了unroot的位置,jsFunc构造的时候,_autoRootUnroot为true。所以执行se::Object* funcObj = jsFunc.toObject();会有一次root。这样lambda表达式初始化的时候,会多一次root。导致js层释放canvas之后,由于jsFunc的引用导致不能被GC掉。

This comment has been minimized.

Copy link
@minggo

minggo Jul 31, 2019

Contributor

jsFunc构造的时候,_autoRootUnroot为true

构造函数初始化的是 false

Value::Value(Object* o, bool autoRootUnroot/* = false*/)
: _type(Type::Undefined)
, _autoRootUnroot(false)
{
setObject(o, autoRootUnroot);
}

This comment has been minimized.

Copy link
@caryliu1999

caryliu1999 Jul 31, 2019

Author Contributor

jsFunc构造的时候,_autoRootUnroot为true

构造函数初始化的是 false

是 args[0] 这个参数是个Value, 它的autoRootUnroot是true。
image

This comment has been minimized.

Copy link
@minggo

minggo Jul 31, 2019

Contributor

args[0] 应该由自己的析构去 unroot 吧,放在这里 unroot 显得很奇怪啊。jsFunc 只是对它的一个包装而已。

This comment has been minimized.

Copy link
@caryliu1999

caryliu1999 Aug 1, 2019

Author Contributor

这里我后面考虑看能不能用TypedArray进行数据传递,不要用回调的形式了,本身也耗性能。

This comment has been minimized.

Copy link
@minggo

minggo Aug 1, 2019

Contributor

不过lambda表达式对jsFunc造成的root

我没明白的是 lambda 表达式哪里对 jsFunc 进行了 root。

This comment has been minimized.

Copy link
@caryliu1999

caryliu1999 Aug 1, 2019

Author Contributor

不过lambda表达式对jsFunc造成的root

我没明白的是 lambda 表达式哪里对 jsFunc 进行了 root。

lambda 表达式内部只要有引用jsFunc这个局部变量,初始化的时候就会触发一次jsFunc的拷贝构造,然后jsFunc的autoRootUnroot又是true,所以会有一次root。

This comment has been minimized.

Copy link
@minggo

minggo Aug 1, 2019

Contributor

嗯,有道理。加个注释吧,不然不太容易理解。

This comment has been minimized.

Copy link
@caryliu1999

caryliu1999 Aug 2, 2019

Author Contributor

已添加注释。

caryliu1999 added 2 commits Jul 31, 2019
@minggo minggo merged commit 25b2ab8 into cocos-creator:v2.1.3 Aug 2, 2019
1 check was pending
1 check was pending
continuous-integration/travis-ci/pr The Travis CI build is in progress
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.