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

@caryliu1999 caryliu1999 commented Jul 31, 2019

Re: cocos-creator/2d-tasks#1614

@@ -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();
Copy link
Contributor

@minggo minggo Jul 31, 2019

Choose a reason for hiding this comment

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

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

Copy link
Contributor Author

@caryliu1999 caryliu1999 Jul 31, 2019

Choose a reason for hiding this comment

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

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

Copy link
Contributor

@minggo minggo Jul 31, 2019

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@caryliu1999 caryliu1999 Jul 31, 2019

Choose a reason for hiding this comment

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

jsFunc构造的时候,_autoRootUnroot为true

构造函数初始化的是 false

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

Copy link
Contributor

@minggo minggo Jul 31, 2019

Choose a reason for hiding this comment

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

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

Copy link
Contributor Author

@caryliu1999 caryliu1999 Aug 1, 2019

Choose a reason for hiding this comment

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

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

Copy link
Contributor

@minggo minggo Aug 1, 2019

Choose a reason for hiding this comment

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

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

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

Copy link
Contributor Author

@caryliu1999 caryliu1999 Aug 1, 2019

Choose a reason for hiding this comment

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

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

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

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

Copy link
Contributor

@minggo minggo Aug 1, 2019

Choose a reason for hiding this comment

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

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

Copy link
Contributor Author

@caryliu1999 caryliu1999 Aug 2, 2019

Choose a reason for hiding this comment

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

已添加注释。

@minggo minggo merged commit 25b2ab8 into cocos-creator:v2.1.3 Aug 2, 2019
1 check was pending
@caryliu1999 caryliu1999 deleted the v2.1.3-fix-canvas branch Jun 22, 2021
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

2 participants