gpt4 book ai didi

java - jni - 应用程序崩溃

转载 作者:行者123 更新时间:2023-11-29 09:14:29 25 4
gpt4 key购买 nike

这是一个与NDK和Android SDK相关的问题。目前,我的 C 代码正在调用 Java 中定义的方法。此方法称为 callFromNDK()。

在这个方法中,我引用了媒体播放器的一个实例来播放一段简短的测试音。 callFromNDK() 每 2 秒调用一次。而测试音本身就是一秒。

我看到的是 (SIGSEGV),代码 1 (SEGV_MAPERR),有时出现故障地址 fffffff4。

我想知道发生这种情况是否是因为媒体播放器实例是在与使用它的上下文不同的上下文中创建的?

这是相关的一段代码[Java文件]

public class Canvastutorial extends Activity {
private static MediaPlayer mediaPlayer = null;
public void callFromNDK() {
if (mediaPlayer != null) {
mediaPlayer.start();
}
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.audioclip);
mediaPlayer.setLooping(false);
}

@Override
protected void onDestroy() {
mediaPlayer.release();
mediaPlayer = null;
System.gc();
super.onDestroy();
}

@Override
protected void onPause() {
stopAndPrepare();
super.onPause();
}

private void stopAndPrepare() {
mediaPlayer.stop();
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.seekTo(0);
}
}

应用崩溃时的LogCat如下:

04-27 10:42:13.228: I/DEBUG(8926): Build fingerprint: 'google/passion/passion:2.3.6/GRK39F/189904:user/release-keys'
04-27 10:42:13.228: I/DEBUG(8926): pid: 10362, tid: 11222 >>> <package_name> <<<
04-27 10:42:13.228: I/DEBUG(8926): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr fffffff4
04-27 10:42:13.238: I/DEBUG(8926): r0 4214cdc0 r1 00000000 r2 00000000 r3 0000ce68
04-27 10:42:13.238: I/DEBUG(8926): r4 44628398 r5 00000000 r6 4472fbd8 r7 0000000e
04-27 10:42:13.238: I/DEBUG(8926): r8 80018000 r9 00000000 10 00000000 fp 800a5368
04-27 10:42:13.238: I/DEBUG(8926): ip 00000000 sp 4472fb88 lr 8001d084 pc 8001d090 cpsr 60000010
04-27 10:42:13.238: I/DEBUG(8926): d0 00650072006800b8 d1 00000044bed7f457
04-27 10:42:13.238: I/DEBUG(8926): d2 0069006400650052 d3 004d0049002e0040
04-27 10:42:13.238: I/DEBUG(8926): d4 0061006900640065 d5 00790061006c0050
04-27 10:42:13.238: I/DEBUG(8926): d6 0065005300720065 d7 0063006900760072
04-27 10:42:13.238: I/DEBUG(8926): d8 0000000000000000 d9 0000000042ba56de
04-27 10:42:13.238: I/DEBUG(8926): d10 0000000000000000 d11 0000000000000000
04-27 10:42:13.238: I/DEBUG(8926): d12 0000000000000000 d13 0000000000000000
04-27 10:42:13.238: I/DEBUG(8926): d14 0000000000000000 d15 0000000000000000
04-27 10:42:13.238: I/DEBUG(8926): d16 0000000000000001 d17 c053000000000000
04-27 10:42:13.238: I/DEBUG(8926): d18 0000000000000000 d19 0000000000000000
04-27 10:42:13.238: I/DEBUG(8926): d20 3ff0000000000000 d21 8000000000000000
04-27 10:42:13.238: I/DEBUG(8926): d22 0000000000000000 d23 ff00000000000000
04-27 10:42:13.238: I/DEBUG(8926): d24 ff00000000000000 d25 ff00000000000000
04-27 10:42:13.238: I/DEBUG(8926): d26 0100010001000100 d27 0100010001000100
04-27 10:42:13.238: I/DEBUG(8926): d28 0100010001000100 d29 3ff0000000000000
04-27 10:42:13.238: I/DEBUG(8926): d30 0000000000000000 d31 3ff0000000000000
04-27 10:42:13.238: I/DEBUG(8926): scr 60000012
04-27 10:42:13.328: I/DEBUG(8926): #00 pc 0001d090 /system/lib/libdvm.so
04-27 10:42:13.328: I/DEBUG(8926): #01 pc 000220e4 /system/lib/libdvm.so
04-27 10:42:13.328: I/DEBUG(8926): #02 pc 00020fdc /system/lib/libdvm.so
04-27 10:42:13.328: I/DEBUG(8926): #03 pc 0005fc40 /system/lib/libdvm.so
04-27 10:42:13.328: I/DEBUG(8926): #04 pc 0004cff8 /system/lib/libdvm.so
04-27 10:42:13.328: I/DEBUG(8926): #05 pc 00001590 /data/data/<package_name>/lib/libtest-jni.so
04-27 10:42:13.328: I/DEBUG(8926): #06 pc 000016ca /data/data/<package_name>/lib/libtest-jni.so
04-27 10:42:13.328: I/DEBUG(8926): #07 pc 000118e4 /system/lib/libc.so
04-27 10:42:13.328: I/DEBUG(8926): #08 pc 000114b0 /system/lib/libc.so
04-27 10:42:13.328: I/DEBUG(8926): code around pc:
04-27 10:42:13.328: I/DEBUG(8926): 8001d070 fa0108cc ea000017 e3a00001 e3a09000
04-27 10:42:13.328: I/DEBUG(8926): 8001d080 ebffff72 e2450014 e5905000 e5909004
04-27 10:42:13.328: I/DEBUG(8926): 8001d090 e515200c e5963018 e3520000 1592a000
04-27 10:42:13.328: I/DEBUG(8926): 8001d0a0 e3a01000 0affff8a e1f970b6 e5862010
04-27 10:42:13.328: I/DEBUG(8926): 8001d0b0 e59a1028 e5835028 e590a010 e1a04009
04-27 10:42:13.328: I/DEBUG(8926): code around lr:
04-27 10:42:13.328: I/DEBUG(8926): 8001d064 e088f30c e1a01000 e5960018 fa0108cc
04-27 10:42:13.328: I/DEBUG(8926): 8001d074 ea000017 e3a00001 e3a09000 ebffff72
04-27 10:42:13.328: I/DEBUG(8926): 8001d084 e2450014 e5905000 e5909004 e515200c
04-27 10:42:13.328: I/DEBUG(8926): 8001d094 e5963018 e3520000 1592a000 e3a01000
04-27 10:42:13.328: I/DEBUG(8926): 8001d0a4 0affff8a e1f970b6 e5862010 e59a1028
04-27 10:42:13.328: I/DEBUG(8926): stack:
04-27 10:42:13.328: I/DEBUG(8926): 4472fb48 4472fbe0
04-27 10:42:13.328: I/DEBUG(8926): 4472fb4c 4214cce4
04-27 10:42:13.328: I/DEBUG(8926): 4472fb50 0000ce60
04-27 10:42:13.328: I/DEBUG(8926): 4472fb54 00000001
04-27 10:42:13.328: I/DEBUG(8926): 4472fb58 4472fbe0
04-27 10:42:13.338: I/DEBUG(8926): 4472fb5c 80049697 /system/lib/libdvm.so
04-27 10:42:13.338: I/DEBUG(8926): 4472fb60 4214cce4
04-27 10:42:13.338: I/DEBUG(8926): 4472fb64 431fbec9
04-27 10:42:13.338: I/DEBUG(8926): 4472fb68 ad34675d /system/lib/libandroid_runtime.so
04-27 10:42:13.338: I/DEBUG(8926): 4472fb6c 4472fbe0
04-27 10:42:13.338: I/DEBUG(8926): 4472fb70 42f8e91e
04-27 10:42:13.338: I/DEBUG(8926): 4472fb74 4214cd00
04-27 10:42:13.338: I/DEBUG(8926): 4472fb78 4472fbd8
04-27 10:42:13.338: I/DEBUG(8926): 4472fb7c 40038360
04-27 10:42:13.338: I/DEBUG(8926): 4472fb80 df002777
04-27 10:42:13.338: I/DEBUG(8926): 4472fb84 e3a070ad
04-27 10:42:13.338: I/DEBUG(8926): #00 4472fb88 4214e32c
04-27 10:42:13.338: I/DEBUG(8926): 4472fb8c 4472fbd8
04-27 10:42:13.338: I/DEBUG(8926): 4472fb90 00000001
04-27 10:42:13.338: I/DEBUG(8926): 4472fb94 002c2c40
04-27 10:42:13.338: I/DEBUG(8926): 4472fb98 0000ce68
04-27 10:42:13.338: I/DEBUG(8926): 4472fb9c 000f45b8
04-27 10:42:13.338: I/DEBUG(8926): 4472fba0 800aad38
04-27 10:42:13.338: I/DEBUG(8926): 4472fba4 fffffe84
04-27 10:42:13.338: I/DEBUG(8926): 4472fba8 800a5368
04-27 10:42:13.338: I/DEBUG(8926): 4472fbac 800220e8 /system/lib/libdvm.so
04-27 10:42:13.338: I/DEBUG(8926): #01 4472fbb0 4472fbd8
04-27 10:42:13.338: I/DEBUG(8926): 4472fbb4 0000ce60
04-27 10:42:13.338: I/DEBUG(8926): 4472fbb8 80022058 /system/lib/libdvm.so
04-27 10:42:13.338: I/DEBUG(8926): 4472fbbc 423298c8
04-27 10:42:13.338: I/DEBUG(8926): 4472fbc0 00000000
04-27 10:42:13.338: I/DEBUG(8926): 4472fbc4 80020fe0 /system/lib/libdvm.so

任何想法都会受到赞赏。

谢谢。

项目的 native 端:定期调用 thread_function(2 秒间隔),这就是我调用 Java 代码中定义的函数的地方。

// Callbacks to Android
JavaVM *j_vm;
jobject *j_obj;
JNIEnv *j_env;
jclass j_cls;
jmethodID android_call;

int JNI_OnLoad(JavaVM* vm, void* reserved) {
j_vm = vm;
(*j_vm)->GetEnv(j_vm, (void**) &j_env, JNI_VERSION_1_6);
j_cls = (*j_env)->FindClass(j_env, "<package_name>/<class_name>");
android_call = (*j_env)->GetMethodID(j_env, j_cls, "callFromNDK", "()V");
j_obj = (*j_env)->NewGlobalRef(j_env, (*j_env)->NewObject(j_env, j_cls, android_call));
return JNI_VERSION_1_6;
}

void JNI_OnUnload(JavaVM *vm, void *reserved) {
(*j_env)->DeleteGlobalRef(j_env, j_obj);
}

void *thread_function(void *ptr) {
int *which = (int *) ptr;
(*j_vm)->AttachCurrentThread(j_vm, &j_env, NULL);
int rc;
while (!stop_thread) {
rc = pthread_mutex_lock(&mtx);
rc = pthread_cond_wait(&cond, &mtx);
rc = pthread_mutex_unlock(&mtx);
if (!stop_thread) {
(*j_env)->CallVoidMethod(j_env, j_obj, android_call);
}
}
(*j_vm)->DetachCurrentThread(j_vm);
return NULL;
}

最佳答案

您是否尝试过在模拟器中运行它?由于它在 libdvm 中崩溃,您可能会在 native 崩溃之前获得一些非常有用的日志记录。

问题可能出在以下行:

j_cls = (*j_env)->FindClass(j_env, "<package_name>/<class_name>");

假设您更换了 "<package_name>/<class_name>" 是否安全?使用实际的命名空间和类名?

关于java - jni - 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10354236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com