论坛首页 移动开发技术论坛

cocos2dx fatal signal11(SIGSEGV) at 0x00000000(code=1)的解决实践

浏览 3693 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2013-12-06  
产品基于cocos2dx开发的棋牌游戏。

出现的问题:如果app游戏时正好网络处于开启状态,则运行正常。反之,则在android logcat输出signal11(SIGSEGV) at 0x00000000(code=1)

<pre name="code" class="java">
12-06 14:59:22.482: I/dalvikvm(8782): dvmDdmHandleHpsgChunk(when 0, what 0, heap 0)
12-06 14:59:23.376: I/dalvikvm(8782): Debugger is active
12-06 14:59:44.266: D/Cocos2dxActivity(8782): model=HUAWEI U9510E
12-06 14:59:44.266: D/Cocos2dxActivity(8782): product=U9510E
12-06 14:59:44.266: D/Cocos2dxActivity(8782): isEmulator=false
12-06 14:59:45.873: W/System.err(8782): /data/system/carrierinfo.prop: open failed: ENOENT (No such file or directory)
12-06 14:59:45.879: W/System.err(8782): java.net.UnknownHostException: Unable to resolve host "feedback.umeng.com": No address associated with hostname
12-06 14:59:45.886: W/System.err(8782): at java.net.InetAddress.lookupHostByName(InetAddress.java:426)
12-06 14:59:45.889: W/System.err(8782): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
12-06 14:59:45.893: W/System.err(8782): at java.net.InetAddress.getAllByName(InetAddress.java:220)
12-06 14:59:45.899: W/System.err(8782): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
12-06 14:59:45.903: W/System.err(8782): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
12-06 14:59:45.903: W/System.err(8782): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
12-06 14:59:45.913: W/System.err(8782): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
12-06 14:59:45.916: W/System.err(8782): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-06 14:59:45.923: W/System.err(8782): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-06 14:59:45.929: W/System.err(8782): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-06 14:59:45.939: W/System.err(8782): at com.umeng.fb.a.b.a(FbClient.java:189)
12-06 14:59:45.943: W/System.err(8782): at com.umeng.fb.model.Conversation$1.run(Conversation.java:219)/data/system/carrierinfo.prop: open failed: ENOENT (No such file or directory)
12-06 14:59:45.946: W/System.err(8782): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-06 14:59:45.946: W/System.err(8782): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-06 14:59:45.946: W/System.err(8782): at java.lang.Thread.run(Thread.java:856)
12-06 14:59:45.949: W/System.err(8782): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
12-06 14:59:45.966: W/System.err(8782): at libcore.io.Posix.getaddrinfo(Native Method)
12-06 14:59:45.966: W/System.err(8782): at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:56)
12-06 14:59:45.969: W/System.err(8782): at java.net.InetAddress.lookupHostByName(InetAddress.java:411)
12-06 14:59:45.969: W/System.err(8782): ... 14 more
12-06 14:59:45.976: W/System.err(8782): /data/system/carrierinfo.prop: open failed: ENOENT (No such file or directory)
12-06 14:59:47.446: A/libc(8782): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1)
</pre>

完整的排错过程如下
1, android debug模式查看具体出错的问题
   貌似不是在android层, 确定为cpp代码出了问题;

2. appdelegate.cpp
   CCLog("test")
   没有打印出来,仍然直接报错

3.  继续log输出
     System.loadLibrary("game");
  Log.i(TAG,"android loadLibary done");
  logcat能够输出相关代码,说明android层的代码都执行了,现在代码运行到了cpp层;

4. 继续跟踪jni/game/main.cpp
      Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit
   printf("test")
   log cat没有显示print信息
       这里一直对cocos2dx在android输出存在疑问。后来在application.mk添加了相关的设置仍然看不到log输出。

5. 采用
      coco2d:cclog("test")
     情况依旧

6.  开始排除jpush第三方推送jar, so库是否存在问题。
    删除libs/libsys.so (jpush的so文件)
    删除jpushInterface.init(this)

7. 继续在jni/game/main.cpp
    使用ccmessagebox();
    没有显示弹出窗口提示;

8. 修改application.mk
    LOCAL_CFLAGS += -DCOCOS2D_DEBUG=2
   没有结果,问题仍然存在
  
9.  libegl  loaded /system/lib.egl/libegl_viviante.so,
    如果关闭网络,则没有显示加载上述的so文件
       

10. 打开jpush.setDebug(true);
    在无网络情况下,显示sis error

11. 使用ndk-stack来查找崩溃
       ./adb logcat|ndk-stack -sym /Users/chenxu/work/c++/cocos2d-x-2.1.4/work/doudizhu/proj.android/obj/local/armeabi/     
     
<pre name="code" class="java">
       ********** Crash dump: **********
Build fingerprint: 'Huawei/U9510E/hwu9510e:4.0.4/HuaweiU9510E/CHNC00B122:user/ota-rel-keys,release-keys'
pid: 27322, tid: 27322  &gt;&gt;&gt; com.nbgame.doudizhu &lt;&lt;&lt;
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000000
Stack frame #00  pc 003b8158  /data/data/com.nbgame.doudizhu/lib/libgame.so (_ZN12GameProtocal19createServerMessageEi): Routine createServerMessage in /Users/chenxu/work/c++/cocos2d-x-2.1.4/work/doudizhu/proj.android/../../../baisonext/NetWork/GameProtocal.cpp:46
Stack frame #01  pc 003b7dcc  /data/data/com.nbgame.doudizhu/lib/libgame.so (_ZN7Network20dispatchNetworkErrorEi): Routine dispatchNetworkError in /Users/chenxu/work/c++/cocos2d-x-2.1.4/work/doudizhu/proj.android/../../../baisonext/NetWork/Network.cpp:319
</pre>

真正崩溃原因,这次是冤枉了jpush等第三方库,是android工程编写有问题。
原因是android启动后立即监控网络状态,如果网络为断开状态,则通过jni call cpp, 后者调用dispatchNetwork()函数,通过创建一个message丢入队列,界面线程pop消息之后,画出网络错误提示界面提示。
    
      问题在于:这些都是多线程处理的,即android层由独立线程监控网络状态, cocos2dx界面层由独立线程运行,其中后者在收到网络错误时候,需要有关的未初始化实例来创建消息,导致了空指针的错误。 fatal signal 11(SIGSEGV)

      最后解决办法:只有确定网络ok, 并且用户成功登陆后,cpp层采取调用原生android层,去监听网络状态。这样可以避免前面网络未建立就立刻通知界面的错误;
          
      bak-stack是个好东西,对于android异常崩溃能够提供call-stack,有效确定代码的出错位置;
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics