Anyone can prove JNI is safe? My application crashes after many calls.
843829Mar 14 2003 — edited Apr 8 2005I heard that JNI is not safe. In fact I made an application base on many JNI calls.
C level is API supplied by IBM. And I was told the C API is tested and safe.
Java application and JNI calls is writen by myself. It crashed after about 3000 times call. I tried other JDK and other OS. On some platform, the thread hold there after many calls. No error, no crash. But others crashed even early.
Here's my JNI call's code:
JNIEXPORT void JNICALL Java_com_ibm_mm_sdk_cim_internal_PServiceFACImp_cEvaluate
(JNIEnv *env, jclass jobj, jlong jhandle, jstring jmanualedListFilename, jstring jclassedListFileName, jstring jresultFilename){
char *manualedListFilename = jstringToWindows(env, jmanualedListFilename);
char *classedListFileName = jstringToWindows(env, jclassedListFileName);
char *resultFilename = jstringToWindows(env, jresultFilename);
int rc; // API return code
void* handle; // FAC Handle
// convert C pointer from java long type
handle = (void*)jhandle;
rc = KmFAC_Evaluate(
handle, // FAC Handle
(char*)manualedListFilename, // File name of list of files
(char*)classedListFileName, // File name of list of files
(char*)resultFilename // Result file
);
if (manualedListFilename)
free(manualedListFilename);
if (classedListFileName)
free(classedListFileName);
if (resultFilename)
free(resultFilename);
if (rc != KM_RC_OK) {
//Throw exception
jclass exceptClass; // JNI exception class
char errorMsg[256]; // Used to build error msg for exceptions
jmethodID methodid; // Exception constructor method id
sprintf(errorMsg, "KmFAC_Evaluate() [CKM=%d]", (int)rc);
exceptClass = env->FindClass("com/ibm/mm/sdk/cim/DKServiceExceptionCIM");
methodid = env->GetMethodID(exceptClass,"<init>","(Ljava/lang/String;II)V");
jstring str = env->NewStringUTF(errorMsg);
jthrowable excobj = (jthrowable)env->NewObject(exceptClass,methodid,str,100, (int)rc);
env->Throw(excobj);
}
return;
}