JNI可能是一个真正的头痛。你的表面看起来很好看。
首先,我注意到你没有使用offset
- 这是一种代码味道。
其次,你没有释放UChar阵列。
第三,C函数或赋值循环可能会溢出数组边界。
为了帮助定位突然崩溃这样的,我已经成功地与控制台一起使用的好醇派print
声明。
首先我添加一个println方法我JNIGlobal类:
/** Print text or ASCII byte array prefixed with "JNI: ". Primarily for native code to output to the Java console. */
static public void println(Object val) {
if(val instanceof byte[]) { byte[] ba=(byte[])val; val=new String(ba,0,ba.length); }
System.out.println("JNI: "+val);
}
然后,我添加一个对应方法我的C代码:
void println(JNIEnv *jep, byte *format,...) {
va_list vap;
byte txt[5001];
jsize txtlen;
jclass eCls;
jint mId;
jbyteArray jText;
va_start(vap,format); vsprintf(txt,format,vap); va_end(vap);
txtlen=(long)strlen(txt);
if((eCls=(*jep)->FindClass(jep,"<your/package/here/JNIGlobal"))==0) {
printf("JNI: Global class not found (Error Text: %s)\n",txt);
return; /* give up */
}
if((mId=(*jep)->GetStaticMethodID(jep,eCls,"println","(Ljava/lang/Object;)V"))==0) {
printf("JNI: Global println method not found (Error Text: %s)\n",txt);
return; /* give up */
}
jText=(*jep)->NewByteArray(jep,txtlen);
(*jep)->SetByteArrayRegion(jep,jText,0,txtlen,(void*)txt);
(*jep)->CallStaticVoidMethod(jep,eCls,mId,jText);
}
然后我打电话println(env,"Some formatted output")
在每一行来源看它有多远。在我的环境(一个AS/400)中,当JVM在交互式运行期间崩溃时,我只剩下控制台 - 您可能希望在Java代码中添加一小段延迟,以确保在控制台消失之前看到输出。
因此,对于你,就像这样:
static jint testFunction(JNIEnv* env, jclass c, jobject obj,
jcharArray chsArray, int offset, int len, jcharArray dstArray) {
/**/println("** testFunction 1");
jchar* dst = env->GetCharArrayElements(dstArray, NULL);
/**/println("** testFunction 2");
if (dst != NULL) {
/**/println("** testFunction 3");
UChar *str = new UChar[len];
/**/println("** testFunction 4");
//populate str here from an ICU4C function
/**/println("** testFunction 5");
for (int i=0; i<len; i++)
dst[i] = str[i]; //this might be the problematic piece of code (can I issue an assignment like this?)
}
/**/println("** testFunction 6");
}
env->ReleaseCharArrayElements(dstArray, dst, 0);
/**/println("** testFunction 7");
}
您已经尝试了调试器附加到正在运行的Java程序调用JNI代码,把一个断点在第一行,并通过它一步? – 2011-02-15 20:40:27