这是由于访问了错误的地址或者内存越界所导致的错误。一般会伴随着Native层的异常。 可以通过调试或者Crash日志等手段来排查这个问题。 代码示例:
public class NativeCrashActivity extends AppCompatActivity {
private TextView crashText;
static {
System.loadLibrary("native-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_native_crash);
crashText = findViewById(R.id.crash_text);
//调用Native方法,模拟出现Segmentation fault错误
int[] array = {0, 1};
int result = funcWithSegmentationFault(array);
Log.d("NativeCrashActivity", "result: " + result);
}
public void onCrashClick(View view) {
//调用Native方法,模拟出现SIGSEGV错误
int[] array = null;
int result = funcWithSIGSEGV(array);
Log.d("NativeCrashActivity", "result: " + result);
}
public void onCrashInThreadClick(View view) {
//创建子线程调用Native方法,模拟出现SIGSEGV错误
new Thread(new Runnable() {
@Override
public void run() {
int[] array = null;
int result = funcWithSIGSEGV(array);
Log.d("NativeCrashActivity", "result: " + result);
}
}).start();
}
//Native方法,模拟出现SIGSEGV错误
public native int funcWithSIGSEGV(int[] array);
//Native方法,模拟出现Segmentation fault错误
public native int funcWithSegmentationFault(int[] array);
}
JNIEXPORT jint JNICALL
Java_com_example_nativecrash_NativeCrashActivity_funcWithSIGSEGV(JNIEnv *env, jobject thiz,
jintArray array) {
jsize length = (*env)->GetArrayLength(env, array);
jint *arr = (*env)->GetIntArrayElements(env, array, NULL);