jvm在使用jni从java类读取arraylist时崩溃

时间:2010-12-04 08:33:20

标签: java c crash jvm java-native-interface

我在使用jni从java类中读取arraylist时遇到了麻烦。在java类中,arraylist的定义如下

public static List XTN_STC_search_result = new ArrayList();

然后,arraylist将填充一个字符串,一个得分值为double和一个整数数组。 所以结构看起来像这样:

[
  [label str 1, score value 1, [int 1, int 2, int n]],
  [label str 2, score value 2, [int 1, int 2],
  ...
]

在JNI C方面,我执行以下操作来阅读该arraylist。

在读取静态字段并获取对象后,我进入循环迭代数组以读取所有值。一切顺利,我可以读取每个arraylist条目,标签字符串,double值,并可以使用嵌套循环访问整数数组。在嵌套循环中,如果使用getObjectArrayElement获取整数数组条目,它似乎获得该值。之后我在NULL上测试它并且它不是NULL。为了迂腐,我仔细检查该数组条目(整数数组)是否是带有isInstanceOf的Integer实例。那个叫做SIGSEGV和JVM的程序崩溃了。

下面你会看到问题区域的代码片段......

int 
XTN_ce_java_get_cluster(XTN_VM_IDX vm_idx) {

...

/* java result object , the arraylist to read */
jobjectArray result_obj;    
/* result object array (element of result_obj) */
jobjectArray jv_result_object_array;        
jint jv_result_object_array_size;
/* element to hold phrase, score and nested integer array */
jobject jv_object_array_cluster_elem;   
jint jv_object_array_cluster_elem_size;

...


/* get arraylist */
jv_result_object_array = (*(Record.env))->GetObjectArrayElement(
    Record.env, result_obj, 1 );
jv_result_object_array_size = (*(Record.env))->GetArrayLength(Record.env,
    jv_result_object_array );

...

/* loop over arraylist entries */
for (i = 0; i < jv_result_object_array_size; i++) {

/* read label string */
if ( (*(Record.env))->IsInstanceOf(Record.env, 
    (jobject)jv_object_array_cluster_elem, classString)) {

...

} else if ( (*(Record.env))->IsInstanceOf(Record.env, jv_object_array_cluster_elem, classObject)) {
/* we are about to read the integer array from the arraylist ... */

/* get size of integer array */
jv_object_array_cluster_elem_size = (*(Record.env))->GetArrayLength(
    Record.env, jv_object_array_cluster_elem);
/* this case should not happen, the integer array has a minimum entry of 1 element */
if (jv_object_array_cluster_elem_size <= 0)
    continue;

for (j = 0; j < jv_object_array_cluster_elem_size; j++) {
    /* get element from integer array */
    jv_cluster_data_array_elem = (*(Record.env))->GetObjectArrayElement(
    Record.env, jv_object_array_cluster_elem, j);
    /* check if null */
    if (jv_cluster_data_array_elem == NULL) {
       break;

    /* now check type */
    /* THIS CALL CRASHES JVM */
    if ( (*(Record.env))->IsInstanceOf(Record.env, jv_cluster_data_array_elem, 
       classInteger)) {
       fprintf(stdout, "got array entry of type integer */
    }

    ...

} /* inner loop integer array */

} /* outer loop object array */

return 1;
}

我不明白它为什么会在那里崩溃,即使我似乎无法访问该字段进行读取(例如使用GetIntField())。如果我省略了isInstanceOf调用(只是循环遍历整数数组)一切顺利,它会循环遍历完整的arraylist,读取我的字符串和双精度数,循环遍历嵌套的整数数组并成功完成。

。老实说

是否有人暗示我要去哪里或要改善什么?我希望提供的arraylist reader函数的代码片段足以理解我的问题。也许有人可以指出我在这个问题上的正确方向,希望: - ))

我用 JRE版本:6.0_22-b04 Java VM:Java HotSpot(TM)64位服务器VM(17.1-b03混合模式linux-amd64) 在Ubuntu上

非常感谢您提前帮助我解决这个问题。

Update 2010/12/06:
================== 
As Peter suggested, I completely reworked the object reader function using the 
Java methods of the object through the native interface. Additionally it 
turned out, that I even do not need that very arraylist I constructed in 
the Java class, I rather used and read the internal class Results Object 
List directly and its object methods to read the results I need. So in short, 
now it is more efficient and I was able to skip that Arraylist construction 
completey and spare one processing step.

First I thought, using the Java methods of that very class through JNI would 
give me a much higher response time, using Java methods through JNI is surely 
not very fast (compared to using the Java methods in Java directly). Using JNI 
as a bridge from C to Java costs performance and computing time. But anyway, I 
figured out, I am about 200ms faster (using the same data set) than my old 
reader method with the arraylist, so I even gained some performance.
Ok, to put it in short, using JNI and the Object methods of the Java class 
spared me a lot of headache and the code now is much more readable. 

Conclusion: I would like to encourage everybody that is facing a similar 
problem using the Object methods of the class you work with through JNI.

1 个答案:

答案 0 :(得分:1)

首先要注意的是ArrayList!=一个数组。用于访问arraylist的方法仅适用于数组。

我建议您通过调用List的方法来访问ArrayList,就像在Java中一样。