cocos2d / cocos2d-js

cocos2d-x for JS
http://www.cocos2d-x.org
MIT License
1.86k stars 490 forks source link

Add cc.textureCache.addImageAsync API to ensure API consistency #1507

Closed pandamicro closed 9 years ago

pandamicro commented 9 years ago

For the web engine, cc.textureCache.addImage is always asynchronous, so we have merged addImage and addImageAsync API. But it turns out the difference of addImage and addImageAsync cannot be ignored.

So it's better to do the following:

  1. [Web Engine] Add addImageAsync API which is equivalent to addImage
  2. [Native Engine] Change the current addImage binding to addImageAsync, then expose the original synchronous addImage API
dingpinglv commented 9 years ago

The addImageAsync has been added to cc.textureCache on Web engine at: https://github.com/cocos2d/cocos2d-html5/pull/2722

0xJimmyBeh commented 9 years ago

@pandamicro

cc.textureCache.addImageAsync cause crashed on Android v4.4.4 with unpublished cocos2d-js v3.5 at http://www.cocos2d-x.org/filedown/cocos2d-js-v3.5.zip

working fine on Mac, iOS crashed for first time then not crash after that.

Tested with code below :

 var imageUrlArray = ["http://www.cocos2d-x.org/s/upload/v35.jpg", "http://www.cocos2d-x.org/s/upload/testin.jpg", "http://www.cocos2d-x.org/s/upload/geometry_dash.jpg"];

    for (var i = 0; i < imageUrlArray.length; i++)
    {
        cc.textureCache.addImageAsync(imageUrlArray[i], this.imageLoaded, this);
    }
pandamicro commented 9 years ago

Thank you, I reproduced an issue, was the crash you met triggered by the following line ?

    JSAutoCompartment ac(_cx, _obj.ref() ? _obj.ref() : global);
0xJimmyBeh commented 9 years ago

There is no error show on the console log in Cocos Code IDE 1.2.0 when debug with Android device, working fine on Mac.

Is there any setup to show the error log?

pandamicro commented 9 years ago

I think the problem is due to thread & curl & Spidermonkey, I will try to find a more reliable solution

0xJimmyBeh commented 9 years ago

ok, thanks.

Deviller commented 9 years ago

I can confirm that there is some bug in the line: JSAutoCompartment ac(_cx, _obj.ref() ? _obj.ref() : global); In function __JSDownloaderDelegator::onSuccess

It crashes often on android and rarely on iOS

Do you have any fix for this?

0xJimmyBeh commented 9 years ago

@Deviller

Thanks for your report on the problem. I opened an issue about this problem at here

https://github.com/cocos2d/cocos2d-js/issues/1658

@pandamicro have you found any solution?

Thanks.

zhaijialong commented 9 years ago

I think this problem is caused by thread safe. I didn't look into the code. is onSuccess run in a new thread? If it does and invokes js api , it's not safe. Maybe we should use Scheduler::performFunctionInCocosThread or something similar. And another way is we use a thread safe buildt SpiderMonkey, and wrap the js api calls in a:

JS_BeginRequest(cx);
...
JS_EndRequest(cx);
pandamicro commented 9 years ago

Yes, it's caused by another thread invoking JS API. I will try Scheduler::performFunctionInCocosThread solution