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

Prevent connection objects from being GC, when they are not done yet. #1716

Merged
merged 3 commits into from Apr 19, 2019

Conversation

Projects
None yet
4 participants
@PatriceJiang
Copy link
Contributor

commented Apr 12, 2019

问题描述

当在一个局部作用域中new jsb.Downloader 进行下载时, 如果这个对象没有被引用. 在离开作用域的时候, 这个downloader对象有概率在任务结束前被GC, 下载任务会被取消.

比如下面的情况

///....
    start() {
        // init logic
        this.label.string = this.text;
        let self = this;
        {

            let qqURL = "http://download.cocos.com/CocosCreator/v2.1.0/CocosCreator_v2.1.0_20181127_mac.dmg";
            let dlPath = jsb.fileUtils.getWritablePath() + "data/CocosCreator_v2.1.0_20181127_mac.dmg";

            jsb.fileUtils.removeFile(dlPath);

            let downloader = new jsb.Downloader();

            downloader.setOnTaskError(function(task, errorCode, errorCodeInternal, errorString) {
                console.error("task download error", errorString);
            });

            downloader.setOnTaskProgress(function(task, byteReceived, totalBytesReceived, totalBytesExpected) {
                console.log("downloader progress", totalBytesReceived, "/", totalBytesExpected);
                self.label.string= "download " + (totalBytesReceived/totalBytesExpected * 100) + "%";
            });

            downloader.setOnFileTaskSuccess(function(task) {
                console.log("file downloaded!!");
                self.label.string = "success!";
            });

            downloader.createDownloadFileTask(qqURL, dlPath, "download qq test");
        } 
    }
///....

参考浏览器中XMLHttpRequest 的行为, 下载任务应该确保能够正常结束.

主要改动

Downloader, WebSocket, SocketIO 实例的生命周期和具体下载任务关联. 避免任务结束前对象被GC.

引擎内XMLHttpRequest 已经有正确的处理.

request->onloadstart = [=](){
if (!request->isDiscardedByReset())
{
thiz.toObject()->root();
cb("onloadstart");
}

request->onloadend = [=](){
if (!request->isDiscardedByReset())
{
// SE_LOGD("XMLHttpRequest (%p) onloadend ...\n", request);
cb("onloadend");
thiz.toObject()->unroot();
}
else

@jareguo jareguo requested review from minggo and wuzhiming Apr 14, 2019

@wuzhiming
Copy link
Contributor

left a comment

👍赞,就是自己管理 downloader 和 socket 的生命周期,避免被GC。

@wuzhiming wuzhiming marked this pull request as ready for review Apr 15, 2019

@PatriceJiang PatriceJiang changed the title Prevent connections from being GC, when they are not done yet. Prevent connection objects from being GC, when they are not done yet. Apr 15, 2019

@minggo

This comment has been minimized.

Copy link
Contributor

commented Apr 15, 2019

@jareguo 自动绑定代码现在不是 CI 自动生成的吗?另外jsb_cocos2dx_audioengine_auto_api.js 还有必要吗?

@minggo

minggo approved these changes Apr 15, 2019

@jareguo

This comment has been minimized.

Copy link
Contributor

commented Apr 16, 2019

自动绑定代码现在不是 CI 自动生成的吗?

CI 问题昨天已修复,我看到在构建中了

jsb_cocos2dx_audioengine_auto_api.js 还有必要吗?

这个没必要吗?我们不是一直在用 audioEngine @wuzhiming

@minggo

This comment has been minimized.

Copy link
Contributor

commented Apr 16, 2019

这个是生成 api 文件的,现在有用这个 api 文件做什么吗?最早生成这些文件是为了给之前做的代码编辑器做提示的。

@jareguo

This comment has been minimized.

Copy link
Contributor

commented Apr 16, 2019

这个我不太清楚, @wuzhiming 知道吗?

@wuzhiming

This comment has been minimized.

Copy link
Contributor

commented Apr 16, 2019

这个API的文件目前没什么用了

@minggo

This comment has been minimized.

Copy link
Contributor

commented Apr 16, 2019

那就可以不生成了。

@jareguo jareguo merged commit 6ec1f99 into cocos-creator:v2.1-release Apr 19, 2019

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.