cocos2d / cocos2d-x

Cocos2d-x is a suite of open-source, cross-platform, game-development tools utilized by millions of developers across the globe. Its core has evolved to serve as the foundation for Cocos Creator 1.x & 2.x.
https://www.cocos.com/en/cocos2d-x
18.24k stars 7.05k forks source link

cocos app will crash on back to foreground, if create a label with a font file after back to foreground. #19039

Open sonyps5201314 opened 6 years ago

sonyps5201314 commented 6 years ago

Steps to Reproduce:

  1. git clone https://github.com/umeng/cocos2dx_Component_lua.git to a dir.
  2. cocos2dx_Component_lua.zip extract the zip file to the clone destination dir with overwrite.

3.build the android apk by android studio,deploy it to a arm android phone,debug it. 4.click "分享"->"分享"->"微信". share the message to one your friend,click “返回友盟社会化组件”. 5.the apk process will crash. follow is the crash call stack info: image

drelaptop commented 6 years ago

did you use latest v3 branch included https://github.com/cocos2d/cocos2d-x/pull/19010 ? that PR fix a similar issue. @sonyps5201314

sonyps5201314 commented 6 years ago

I use it, I try to build with the latest cocos git v3 branch version,but the problen is still on. image @drelaptop

drelaptop commented 6 years ago

reproduce this issue, I think the key logs is:

call to OpenGL ES API with no current context (logged once per thread)

in

09-14 12:30:19.068 23189-23189/com.umeng.soexample D/MicroMsg.SDK.WXApiImplV10: registerApp, appId = wxdc1e388c3822c80b
    register app com.umeng.soexample
09-14 12:30:19.075 23189-23189/com.umeng.soexample D/MicroMsg.SDK.MMessage: send mm message, intent=Intent { act=com.tencent.mm.plugin.openapi.Intent.ACTION_HANDLE_APP_REGISTER (has extras) }, perm=com.tencent.mm.permission.MM_MESSAGE
09-14 12:30:19.075 23189-23189/com.umeng.soexample E/UMWXHandler: handleid=com.umeng.socialize.handler.UMWXHandler@79241da
09-14 12:30:19.075 23189-23189/com.umeng.soexample E/umengsocial: 6.7.1umeng_tool----wechat full version:6.7.1
09-14 12:30:19.078 23189-23261/com.umeng.soexample E/umengsocial: xxxxxx platform=WEIXIN
09-14 12:30:19.079 23189-23261/com.umeng.soexample D/cocos2d-x debug info: [LUA-print] #### HelloWorld 分享成功 --> Cocos2d-x SDK 
09-14 12:30:19.079 23189-23261/com.umeng.soexample E/libEGL: call to OpenGL ES API with no current context (logged once per thread)
09-14 12:30:19.147 23189-23261/com.umeng.soexample D/cocos2d-x debug info: create rendererRecreatedListener for GLProgramState
09-14 12:30:19.148 23189-23261/com.umeng.soexample D/cocos2d-x debug info: create rendererRecreatedListener for GLProgramState
09-14 12:30:19.150 23189-23261/com.umeng.soexample D/cocos2d-x debug info: [LUA-print] #### callback!!!!!! 分享成功
    [LUA-print] platform num is : 2,200
09-14 12:30:19.313 23189-23189/com.umeng.soexample D/ViewRootImpl@b00bbac[AppActivity]: Relayout returned: old=[0,0][1920,1080] new=[0,0][1920,1080] result=0x1 surface={valid=false 0} changed=false
09-14 12:30:19.329 23189-23189/com.umeng.soexample D/Cocos2dxActivity: onResume()
sonyps5201314 commented 6 years ago

so can be solved?

drelaptop commented 6 years ago

Sure, don't worry about it. @minggo will help next, he is very experienced on renderer.

minggo commented 6 years ago

I will take a look now.

minggo commented 6 years ago

@sonyps5201314 did you create a new thread to do something? Could you please show me the codes that share the message, and the codes that come back?

minggo commented 6 years ago

Did you invoke engine codes in not GL thread?

minggo commented 6 years ago

Your test case is too difficult to analyze, could you please provide a simple one? Thanks.

sonyps5201314 commented 6 years ago

This test case is already simple enough for me, more difficult case is our corporation project, I am sure the java code is run in the main thread, the cocos code run in the OPENGL thread. so I think it is not a multi-thread problem. you can search "runOnOpenGLThread" and "runOnMainThread" to verify it in path "cocos2dx_Component_lua\frameworks\runtime-src\proj.android-studio\app\src\com\umeng\social\CCUMSocialController.java". and if it run in a emulator like "MuMu模拟器",it will not happen. so I think it is not a multi-thread problem.

minggo commented 6 years ago

Because you are familiar with the codes, so it is not difficult to you. Why not simplify CCUMSocialController.java, remove codes that are not needed, and just keep needed codes. Then it is more helpful.

sonyps5201314 commented 6 years ago

OK, CCUMSocialController.zip this is a simplier CCUMSocialController.java, you can overwrite it. and I can tell you,what is the key code,start share is “directShare“ function, share complete is the code start with:

private static UMShareListener umShareListener = new UMShareListener() {
minggo commented 6 years ago

May be you can have a try of https://github.com/cocos2d/cocos2d-x/pull/19037.

sonyps5201314 commented 6 years ago

都是中国人,我直接说中文吧,说英文费劲。出问题的代码是在CCTextureAtlas.cpp,你上面说那个意思是说没有在EVENT_RENDERER_RECREATED事件时重建OPENGL对象,但是我看CCTextureAtlas.cpp中已经做了相关处理的,会捕获EVENT_RENDERER_RECREATED事件,并创建OPENGL对象,但是这个EVENT_RENDERER_RECREATED事件,从未触发过,沿着这条思路,我通过下断点,及对已有知识的基础上进行猜测。修改了AndroidManifest.xml文件,先是去掉了新近添加的android:launchMode="singleTask" android:taskAffinity="",结果无效,还是不被触发;接着我去掉了android:configChanges中的screenSize,好了问题解决了EVENT_RENDERER_RECREATED会被触发了。不再崩溃了。也就是Cocos2dxRenderer::onSurfaceCreated会被调用了。

Translated by google: The problematic code is in CCTextureAtlas.cpp, which you said above means that the OPENGL object was not rebuilt in the EVENT_RENDERER_RECREATED event, but I have already done the relevant processing in CCTextureAtlas.cpp, it will capture the EVENT_RENDERER_RECREATED event and create the OPENGL object. But this EVENT_RENDERER_RECREATED event has never been triggered. Following this line of thought, I guessed by breaking points and based on existing knowledge. Modified the AndroidManifest.xml file, first removed the newly added android:launchMode="singleTask" android:taskAffinity="", the result is invalid, or not triggered; then I removed the screenSize in android:configChanges, the problem is solved EVENT_RENDERER_RECREATED will be triggered. No longer crashes. That is, Cocos2dxRenderer::onSurfaceCreated will be called.

sonyps5201314 commented 6 years ago

但是沿着@drelaptop的这条日志:E/libEGL: call to OpenGL ES API with no current context (logged once per thread),我觉得可能是OPENGL上下文丢失了,或者无效了。导致的问题。因为我看接下来TextureAtlas::setupVBOandVAO()中调用的OPENGL API都是失败的。可能只需要重建OPENGL 上下文就好了。而不是需要重新OPENGL表面。我估计android:configChanges中加了screenSize时,OPENGL表面不会丢失,但是OPENGL上下文依然会丢失,需要重建。不过我其实不懂OPENGL,可能猜测得也有问题。我也不知道这个上下文丢失,能不能捕获到。你们仔细斟酌后,决定看怎么改吧,改了记得多拿几个手机测试下,不光是安卓。也不光是这个问题,也要看改了这里,其他地方是不是受到了影响。多编译几个开源的游戏和你们自己的商业游戏全面测试下再提交发布。

Translated by google: But along this log of @drelaptop: E/libEGL: call to OpenGL ES API with no current context (logged once per thread), I think the OPENGL context may be missing or invalid. The problem caused. Because I see that the OPENGL API called in TextureAtlas::setupVBOandVAO() is a failure. It may be just a matter of rebuilding the OPENGL context. Instead of needing to re-OPENGL the surface. I estimate that when screenSize is added to android:configChanges, the OPENGL surface will not be lost, but the OPENGL context will still be lost and needs to be rebuilt. However, I don't really understand OPENGL, and I guess there are problems. I don't know if this context is lost and can't be captured. After careful consideration, you decide to change it. Change it and remember to take a few more mobile phones to test it, not just Android. Not only is this problem, but it has to be changed here, and other places have been affected. Compile several open source games and fully test your own commercial games before submitting them.

sonyps5201314 commented 6 years ago

不过真的也很奇怪,改了之后,每次分享之后,创建的Text的位置都会往上移动几个像素。代码都是一样的,写死了的坐标也会移动。 Translated by google: But it's really weird. After changing it, after each share, the position of the created Text will move up a few pixels. The code is the same, and the coordinates that are written dead will also move.

    --标签背景
    local flagBg = cc.Sprite:create("Shop/sp_flag_bg.png")
    flagBg:setPosition(60, 199)
    flagBg:setVisible(flagVisible)
    flagBg:addTo(item)

    if #flagText>0 then
        --标签文本
        local txtFlag = ccui.Text:create(flagText, "fonts/round_body.ttf", flagFontSize)
        txtFlag:setPosition(46, 84)
        txtFlag:enableOutline(cc.c3b(208, 22, 21), 3)
        --txtFlag:setRotation(-45)
        txtFlag:addTo(flagBg)
    end

下面是效果图,请解压后查看,文字是“热 卖”,每次分享后都往上面移动了。 微信图片_20180919063931.zip 难道重建OPENGL表面后,会改变坐标系统? Translated by google: The following is the renderings, please unzip and view, the text is "hot", and move to the top after each share. 微信图片_20180919063931.zip After rebuilding the OPENGL surface, will the coordinate system be changed?

minggo commented 6 years ago

Please paste translated english too. cocos2d-x is a world cross project, it is not only used in China. I will close this issue if you continue speaking only Chinese here.

sonyps5201314 commented 6 years ago

I already translate them above.