C-JNI将2D int数组作为JobjectArray返回

时间:2013-12-13 21:38:05

标签: java c java-native-interface multidimensional-array

所以我决定对我的java代码进行dll导入。

它的作用是计算邻居矩阵。问题是我不知道如何将矩阵作为jobjectArray返回。

JNIEXPORT jobjectArray JNICALL Java_src_pathfinder_PathFinder_neighbourMatrixGenerator (JNIEnv * env, jobject b,...) {
    int i,j;
    BOOL tt=1;

    jclass intClass = (*env)->FindClass(env, "java/lang/Integer");
    jobjectArray row;
    jobjectArray rows;

    int **matrix = (int**)calloc(size, sizeof(int*));

    for(i = 0; i < size; i++) {
        matrix[i] = (int *) calloc(size, sizeof(int));
    }

    for(i=0; i<size; i++) {
        row = (*env)->NewObjectArray(env, size, intClass, 0);

        for(j=0; j<size; j++) {
            (*env)->SetObjectArrayElement(env, row, j, (jobject)matrix[i][j]); // this line is 100% broken
        }

        if(tt==1) {
            tt=0;rows = (*env)->NewObjectArray(env, size, (*env)->GetObjectClass(env, row), 0);
        }

        (*env)->SetObjectArrayElement(env, rows, i, row);
    }

    return rows;
}

我得到的错误:

Loaded generator.dll
counted total rects: 75
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x621e8551, pid=5336, tid=5704
#
# JRE version: Java(TM) SE Runtime Environment (8.0-b75) (build 1.8.0-ea-b75)
# Java VM: Java HotSpot(TM) Client VM (25.0-b17 mixed mode windows-x86 )
# Problematic frame:
# V  [jvm.dll+0xc8551]
#
# Failed to write core dump. Minidumps are not enabled by default on client vers
ions of Windows
#
# An error report file with more information is saved as:
# C:\HnH\hs_err_pid5336.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.sun.com/bugreport/crash.jsp
#

错误日志:http://wklej.to/1T2tW

我很高兴能找到任何可能出错的线索,因为这对我来说是黑魔法

1 个答案:

答案 0 :(得分:0)

似乎矩阵是pSize * pSize,那么首先需要新的pSize数组用于返回,然后需要新数组并存储在returnArray [i]中,并返回第一个数组。

BTW,这里显示的代码是纯java实现的良好候选者。我想如果经常调用它,性能会更好。