Jing Jing - 28 days ago 7
Android Question

Why called CallStaticVoidMethod throwed use of invalid jobject with JNI

I am in JNI for beginners, when I try to call the native method in the MainActivity.in the native method. Static method in CallStaticVoidMethod call MainActivity by native method.Then the program crashes :

this is native method code :

void Java_com_lijing_ndkjnidemeo_activity_MainActivity_staticMethod(JNIEnv *env, jobject obj, jobject aty) {
LOGI("native_staticMethod");
jclass cls;
jmethodID mid;
//1.Get the class method id
cls = (*env)->GetObjectClass(env, aty);
mid = (*env)->GetStaticMethodID(env, cls, "changeStr", "()V");
if (mid == NULL) {
LOGE("GetStaticMethodID error");
return; /* method not found */
}
LOGE("GetStaticMethodID sucess");
//2.Call the CallStatic<ReturnValueType>Method function to call the corresponding function.
(*env)->CallStaticVoidMethod(env, cls, mid);
}


Native method calls in MainActivity:

private void jniTextMethod8() {
staticMethod(this);
tvStaticMethod.setText(sStr);
}

public static native String staticMethod(MainActivity aty);

public static void changeStr() {
sStr = "Change String";
}


Log:

11-07 09:57:35.477 4310-4310/com.lijing.ndkjnidemeo I/JNI_UTIL: native_staticMethod
11-07 09:57:35.477 4310-4310/com.lijing.ndkjnidemeo E/JNI_UTIL: GetStaticMethodID sucess
11-07 09:57:35.493 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] JNI DETECTED ERROR IN APPLICATION: use of invalid jobject 0xffde5e68
11-07 09:57:35.494 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] from java.lang.String com.lijing.ndkjnidemeo.activity.MainActivity.staticMethod(com.lijing.ndkjnidemeo.activity.MainActivity)
11-07 09:57:35.494 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] "main" prio=5 tid=1 Runnable
11-07 09:57:35.494 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] | group="main" sCount=0 dsCount=0 obj=0x73712258 self=0xf3df4a00
11-07 09:57:35.494 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] | sysTid=4310 nice=0 cgrp=default sched=0/0 handle=0xf777cc00
11-07 09:57:35.494 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] | state=R schedstat=( 304952424 35818763 258 ) utm=12 stm=17 core=1 HZ=100
11-07 09:57:35.494 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] | stack=0xff5e8000-0xff5ea000 stackSize=8MB
11-07 09:57:35.494 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] | held mutexes= "mutator lock"(shared held)
11-07 09:57:35.494 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] native: #00 pc 0058b0e2 /system/lib/libart.so (art::DumpNativeStack(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, int, char const*, art::ArtMethod*, void*)+226)
11-07 09:57:35.494 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] native: #01 pc 00550d2e /system/lib/libart.so (art::Thread::Dump(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const+286)
11-07 09:57:35.494 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] native: #02 pc 003a489f /system/lib/libart.so (art::JavaVMExt::JniAbort(char const*, char const*)+1247)
11-07 09:57:35.494 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] native: #03 pc 003a5fc2 /system/lib/libart.so (art::JavaVMExt::JniAbortF(char const*, char const*, ...)+116)
11-07 09:57:35.495 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] native: #04 pc 00548fc7 /system/lib/libart.so (art::Thread::DecodeJObject(_jobject*) const+1975)

Answer

You are not returning anything from your native function.

The native function is defined in java as:

public static native String staticMethod(MainActivity aty);

But in C as

void 
Java_com_lijing_ndkjnidemeo_activity_MainActivity_staticMethod(
    JNIEnv *env,
    jobject obj,
    jobject aty)

You either need to change your Java to

    public static native void staticMethod(MainActivity aty);

or your C to something like:

jobject 
Java_com_lijing_ndkjnidemeo_activity_MainActivity_staticMethod(
    JNIEnv *env,
    jobject obj,
    jobject aty) {
  ..
  return something;
}
Comments