JNI使用(异步条件上)
JNI使用(异步条件下)
1、JNI异步条件下(多线程/回调函数),如何取得JNIEnv
使用AttachCurrentThread()函数。
示例代码:
#ifdef JNI_VERSION_1_4
jint res = cached_jvm->AttachCurrentThread((void**)&env, NULL);
#else
jint res = cached_jvm->AttachCurrentThread(&env, NULL);
#endif;
if (env == NULL)
return;
jclass clsSctp = env->FindClass("com/sunrising/nettest/netpackage/implement/SCTPTransImplement");
if (clsSctp == NULL)
{
ThrowNullPointerException(env, "SCTP class is null");
cached_jvm->DetachCurrentThread();
return;
}
jmethodID mid = env->GetStaticMethodID(clsSctp, "communicationErrorNotify", "(JJ)V");
if (mid == 0) {
ThrowNullPointerException(env, "Method communicationErrorNotify() is null");
cached_jvm->DetachCurrentThread();
return;
}
env->CallStaticVoidMethod(clsSctp, mid, jlAssociationID, jlErrorType);
cached_jvm->DetachCurrentThread();
2、使用GetStringUTFChars()之后要注意释放内存
使用上述函数之后,要使用ReleaseStringUTFChars()函数释放字符串内存。
示例代码:
pBuffer = env->GetStringUTFChars(jsAmrFile, &isCopy);
strncpy_s(szAmrFile, MAX_STRING, pBuffer, MAX_STRING - 1);
if (isCopy & 0xFF)
{
env->ReleaseStringUTFChars(jsAmrFile, pBuffer);
}
1、JNI异步条件下(多线程/回调函数),如何取得JNIEnv
使用AttachCurrentThread()函数。
示例代码:
#ifdef JNI_VERSION_1_4
jint res = cached_jvm->AttachCurrentThread((void**)&env, NULL);
#else
jint res = cached_jvm->AttachCurrentThread(&env, NULL);
#endif;
if (env == NULL)
return;
jclass clsSctp = env->FindClass("com/sunrising/nettest/netpackage/implement/SCTPTransImplement");
if (clsSctp == NULL)
{
ThrowNullPointerException(env, "SCTP class is null");
cached_jvm->DetachCurrentThread();
return;
}
jmethodID mid = env->GetStaticMethodID(clsSctp, "communicationErrorNotify", "(JJ)V");
if (mid == 0) {
ThrowNullPointerException(env, "Method communicationErrorNotify() is null");
cached_jvm->DetachCurrentThread();
return;
}
env->CallStaticVoidMethod(clsSctp, mid, jlAssociationID, jlErrorType);
cached_jvm->DetachCurrentThread();
2、使用GetStringUTFChars()之后要注意释放内存
使用上述函数之后,要使用ReleaseStringUTFChars()函数释放字符串内存。
示例代码:
pBuffer = env->GetStringUTFChars(jsAmrFile, &isCopy);
strncpy_s(szAmrFile, MAX_STRING, pBuffer, MAX_STRING - 1);
if (isCopy & 0xFF)
{
env->ReleaseStringUTFChars(jsAmrFile, pBuffer);
}