I have a question about ndk I have created an Exception in the native module and tried to implement the exception in the java main app, but I encounter SIGTRAP when the app crashes. Do you have any clues?
logcat
12-11 14:12:43.212 28091 28091 F libc : Fatal signal 5 (SIGTRAP), code 1 in tid 28091 (d2002.myndkapp3)
12-11 14:12:43.267 444 444 F DEBUG : pid: 28091, tid: 28091, name: d2002.myndkapp3 >>> com.hhd2002.myndkapp3 <<<
12-11 14:12:43.548 403 403 E lowmemorykiller: Error writing /proc/28091/oom_score_adj; errno=22
12-11 14:12:43.551 474 474 I Zygote : Process 28091 exited due to signal (5)
12-11 14:12:43.588 1231 3425 I ActivityManager: Process com.hhd2002.myndkapp3 (pid 28091) has died
native source
extern "C"
JNIEXPORT jint
JNICALL
Java_com_hhd2002_myndkapp3_MainActivity_throwMyException(
JNIEnv *env,
jobject instance) {
auto exClass = env->FindClass("java/lang/Exception");
env->ThrowNew(exClass, 0);
env->DeleteLocalRef(exClass);
}
java source
public class MainActivity extends AppCompatActivity {
static {
System.loadLibrary("native-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
...
this.findViewById(R.id.btn_0).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
throwMyException();
} catch (Exception e) {
Log.d("mainactivity", e.toString());
}
}
});
}
private native int throwMyException() throws Exception;
}
It was my mistake. The function type of c ++ must have a return value, but that code is missing, So I changed the return to void.
extern "C"
JNIEXPORT void
JNICALL
Java_com_hhd2002_myndkapp3_MainActivity_throwMyException(
JNIEnv *env,
jobject instance) {
auto exClass = env->FindClass("java/lang/Exception");
env->ThrowNew(exClass, 0);
env->DeleteLocalRef(exClass);
}
I was getting SIGTRAP code 1(TRAP_BRKPT) crashes when calling C++ functions that have return values but did not have return statements. I fixed all the crashes by simply adding the proper return statements. This only started happening after I switched from GCC to Clang in Android Studio.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With