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

@PatriceJiang
Copy link
Contributor

@PatriceJiang PatriceJiang 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 已经有正确的处理.

https://github.com/cocos-creator/cocos2d-x-lite/blob/386ddfd4204581ba69f95e2d2f668b7d48f7b43b/cocos/scripting/js-bindings/manual/jsb_xmlhttprequest.cpp#L654-L659

https://github.com/cocos-creator/cocos2d-x-lite/blob/386ddfd4204581ba69f95e2d2f668b7d48f7b43b/cocos/scripting/js-bindings/manual/jsb_xmlhttprequest.cpp#L667-L674

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

@wuzhiming wuzhiming 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
Copy link
Contributor

@minggo minggo commented Apr 15, 2019

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

minggo
minggo approved these changes Apr 15, 2019
@jareguo
Copy link
Member

@jareguo jareguo commented Apr 16, 2019

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

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

jsb_cocos2dx_audioengine_auto_api.js 还有必要吗?

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

@minggo
Copy link
Contributor

@minggo minggo commented Apr 16, 2019

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

@jareguo
Copy link
Member

@jareguo jareguo commented Apr 16, 2019

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

@wuzhiming
Copy link
Contributor

@wuzhiming wuzhiming commented Apr 16, 2019

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

@minggo
Copy link
Contributor

@minggo minggo commented Apr 16, 2019

那就可以不生成了。

@jareguo jareguo merged commit 6ec1f99 into cocos-creator:v2.1-release Apr 19, 2019
1 check passed
@caochao
Copy link

@caochao caochao commented May 27, 2019

很尴尬,现在用2.0.5,合了这个提交的downloader的代码。
把jsb_cocos2dx_network_auto.cpp里的js_network_Downloader_setOnFileTaskSuccess, js_network_Downloader_setOnTaskError两函数及注册删了。
jsb_cocos2dx_network_manual.cpp如下,ios端下载的时候就报错了:
image
image
image
image
image

@jareguo
Copy link
Member

@jareguo jareguo commented May 28, 2019

@caochao 最近关联的改动比较多,只合并一个估计不行。建议升级到 2.0.10 最新版试试,今天会发布,应该会稳定很多。

@PatriceJiang PatriceJiang deleted the fix_gc_issue 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

5 participants