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
Conversation
@@ -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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
之前是哪里进行了 root?另外,thisObj
的值可能为 nullptr。
There was a problem hiding this comment.
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掉。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
jsFunc构造的时候,_autoRootUnroot为true
构造函数初始化的是 false
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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 只是对它的一个包装而已。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里我后面考虑看能不能用TypedArray进行数据传递,不要用回调的形式了,本身也耗性能。
There was a problem hiding this comment.
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。
There was a problem hiding this comment.
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。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
嗯,有道理。加个注释吧,不然不太容易理解。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已添加注释。
Re: cocos-creator/2d-tasks#1614