android - Call Java function from native function based on cached jobject and methodID fail -
java source:
public void oneventlistener(final int eventid, final int arg1, final long arg2, final string message) { log.d("test", "oneventlistener() called - eventid = "+eventid); }
jni code:
javavm* gjvm = null; pid_t _vm_tid = -1; jnienv * jenv = null; jclass native_cls_id = null; jmethodid _oneventlistenerid=null; jobject m_nativeobj = null; jint jni_onload(javavm* vm, void* reserved) { .... gjvm = vm; _vm_tid = gettid(); .... }
//function
void notifyfromnative(int eventid, int arg1, long arg2, char* message) { logd("receive_message_callback called - eventid = %d",eventid); jnienv* env = null; pid_t tid = gettid(); logd("step 0"); if (tid != _vm_tid) { jint ret = gjvm->attachcurrentthread(&env, null); logd("step 1 - tid != _vm_tid ret=%d",ret); } else { logd("step 1 - tid == _vm_tid"); gjvm->getenv((void**) &env, jni_version_1_4); } logd("step 2"); jstring jstr = env->newstringutf(message); logd("step 3"); env->callvoidmethod(m_nativeobj, _oneventlistenerid, eventid, arg1, arg2, jstr); logd("step 4"); env->deletelocalref(jstr); if (tid != _vm_tid) gjvm->detachcurrentthread(); }
//initialize jni function
jniexport jint jnicall java_nativemanager_initialize(jnienv * env, jobject obj) { log_ui("native initialize called"); m_nativeobj = env->newglobalref(obj); jclass cls = env->findclass(classpathname); if (cls == null) { loge("can't find class %s", classpathname); return 0; } native_cls_id = (jclass) env->newglobalref(cls); _oneventlistenerid = env->getmethodid(native_cls_id, "oneventlistener", "(iijljava/lang/string;)v"); if (!_oneventlistenerid) { loge("can't find oneventlistener java method"); return 0; } char* message = "test"; jstring jstr = env->newstringutf(message); env->callvoidmethod(m_nativeobj, _oneventlistenerid, 1, 1, 1, jstr); //this call work notifyfromnative(0, 1, 1, "test"); //<= problem here }
logcat message
oneventlistener() called - eventid = 1 receive_message_callback called - eventid = 0 step 0 step 1 - tid == _vm_tid step 2 step 3 w/dalvikvm(2160): invalid indirect reference 0x576725d0 in decodeindirectref e/dalvikvm(2160): vm aborting a/libc(2160): fatal signal 11 (sigsegv) @ 0xdeadd00d (code=1), thread 2160 ()
question:
can explain me why calling notifyfromnative(0, 1, 1, "test") has problem although global reference of objects created. thank much
i found android jni function can't use long parameter. so, break long value 2 integer values , code working now.
Comments
Post a Comment