Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SIGTRAP occurs when using Exception implemented in NDK

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;
}
like image 458
Hyundong Hwang Avatar asked Nov 30 '22 08:11

Hyundong Hwang


2 Answers

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);
}
like image 71
Hyundong Hwang Avatar answered Dec 04 '22 11:12

Hyundong Hwang


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.

like image 23
android Avatar answered Dec 04 '22 12:12

android