AttachCurrentThread报错

今天早上公司有几个员工反映,小米手机升级到5.0.2系统后,公司APP不能运行

经过一番辛苦 排查后,最后定为在AttachCurrentThread报错,导致程序crash

报错为:

[art/runtime/thread.cc:568] Check failed: &stack_variable > reinterpret_cast<

经过各种谷歌百度,最终发现问题是由于“从 Android 4.4 开始,Google 开发者引进了新的 Android 运行环境 ART(意思就是 Android Runtime。Android 官方页面的介绍中,也将其称作新的虚拟机),以替代旧的 Dalvik VM”

在一篇文章中看到下面这段话:

ART 模式下随机崩溃。Dalvik 对于 native 代码和 Java 代码提供各自的栈,默认 native 栈有 1MB、Java 栈有 32KB。而在 ART 模式下,提供统一的栈。按说,ART 线程栈的大小应该与 Dalvik 的一样。如果你显式设置栈的大小,你可能需要在 ART 模式下运行的 app 里重新访问这些值。

Java 的Thread类有一个构造函数Thread(ThreadGroup group, Runnable runnable, String threadName, long stackSize)提供栈大小参数的设置,如果运行中出现 StackOverflowError 错误,可能需要手动增大 stackSize 值了。

C/C++ 需要调用 POSIX thread 的函数pthread_attr_setstack() 和 pthread_attr_setstacksize()。如果 pthread 栈太小, 调用 JNIAttachCurrentThread() 方法会打印如下 log:

F/art: art/runtime/thread.cc:435] Attempt to attach a thread with a too-small stack (16384 bytes)

我使用AttachCurrentThread语句的线程内部定义了两个数组

char RecvDataBuff[1024000];// 接收数据buffer

char RecvData[1024000]; //返回的数据内容

导致当前线程超过线程默认栈大小,从而导致crash

解决方法:将此两个数组定义为全局变量,问题解决

(转)Android 应用程序升级到 5.0 需要注意的问题

  Android 5.0,代号 Lollipop,源码终于在2014年12月3日放出,国内一大批厂商跟进。最大的改变是默认使用 ART(Android Runtime) ,替换了之前的 Dalvik 虚拟机,提出了 Material Design 界面风格。之前发布的 app 可能需要作一些改动,暂时收集了一些问题,希望对大家有所帮助。

1. Intent/Service  在低于 Android 5.0 版本,程序运行正常。用户抱怨在新的 Android 5.0 设备上崩溃,我们还没有最新的设备,所以暂时用 Android 模拟器调试。在输出的 log 中可以看到这样的记录:

E/AndroidRuntime(26479): java.lang.RuntimeException: Unable to start activity ComponentInfo{PACKAGE_NAME/.ACTIVITY_NAME}: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.bda.controller.IControllerService }

E/GameActivity(18333): Caused by: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.bda.controller.IControllerService }E/GameActivity(18333): at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1982)E/GameActivity(18333): at android.app.ContextImpl.startServiceCommon(ContextImpl.java:2020)E/GameActivity(18333): at android.app.ContextImpl.startService(ContextImpl.java:1995)E/GameActivity(18333): at android.content.ContextWrapper.startService(ContextWrapper.java:533)E/GameActivity(18333): at com.bda.controller.a.d(Unknown Source)

  通过查看堆栈崩溃信息,我们看到使用了第三方的 controller.jar 包导致错误。Controller 是在设备屏幕上模拟游戏手柄功能的包,下载最新的Moga developers SDK,下载了 controller-sdk-std-1.3.1.zip,2013 Feb 01 发布的,有点旧了。里面有 com.bda.controller.jar,没有源码。

尝试 zip 解压 controller.jar 文件,反编译 .class 文件com/bda/controller/BaseController.class想查看 bytecode,使用 javap -c BaseController.class

public final boolean init();Code: 0: aload_0 1: getfield#113// Field mIsBound:Z 4: ifne48 7: new#193// class android/content/Intent 10: dup 11: ldc#165// class com/bda/controller/IControllerService 13: invokevirtual #195// Method java/lang/Class.getName:()Ljava/lang/String; 16: invokespecial #201// Method android/content/Intent."<init>":(Ljava/lang/String;)V 19: astore_1 20: aload_0 21: getfield#142// Field mContext:Landroid/content/Context; 24: aload_1 25: invokevirtual #204// Method android/content/Context.startService:(Landroid/content/Intent;)Landroid/content/ComponentName; 28: pop 29: aload_0 30: getfield#142// Field mContext:Landroid/content/Context; 33: aload_1 34: aload_0 35: getfield#132// Field mServiceConnection:Lcom/bda/controller/Controller$ServiceConnection; 38: iconst_1 39: invokevirtual #208// Method android/content/Context.bindService:(Landroid/content/Intent;Landroid/content/ServiceConnection;I)Z 42: pop 43: aload_0 44: iconst_1 45: putfield#113// Field mIsBound:Z 48: aload_0 49: getfield#113// Field mIsBound:Z 52: ireturn

  你当然想查看源代码,用反编译工具jad,或者临时用网络在线版Show My Code,这个网站可以查看 Zend Guard 加密过的 .php 文件、Java 的 .class 文件、Adobe Flash 的 .swf 文件、.NET 程序 .exe, .dll 或者 QR 二维码,可以收藏一下。

思念带着一种默默地忧伤,

AttachCurrentThread报错

相关文章:

你感兴趣的文章:

标签云: