Tomcat上JNI的致命错误

时间:2015-02-17 17:58:29

标签: java c linux java-native-interface tomcat7

我在做一个矩阵计算器,我要通过JNI做2个矩阵的总和。我通过用户输入得到我的矩阵编号,并将其存储到不同的2D阵列。然后我将两个2d数组转换为单独的1D数组以传递给JNI。但是在完成所有这些之后,服务器停机了,我得到了这个日志文件:

# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007f2bf0c012b1, pid=7021, tid=139824982963968
#
# JRE version: OpenJDK Runtime Environment (7.0_75-b13) (build 1.7.0_75-b13)
# Java VM: OpenJDK 64-Bit Server VM (24.75-b04 mixed mode linux-amd64 compressed oops)
# Derivative: IcedTea 2.5.4
# Distribution: Ubuntu 14.04 LTS, package 7u75-2.5.4-1~trusty1
# Problematic frame:
# V  [libjvm.so+0x5f62b1]  jni_GetDoubleArrayElements+0x51
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# If you would like to submit a bug report, please include
# instructions on how to reproduce the bug and visit:
#   http://icedtea.classpath.org/bugzilla

这是我的.c:

    #include <jni.h>
#include <stdio.h>
#include "MatrixCalculator.h"

JNIEXPORT jdoubleArray JNICALL Java_matrixcalculator_MatrixCalculator_summationOfMatricesViaC
  (JNIEnv * env, jobject thisObj, jint jnirow1, jint jnicolumn1, jdoubleArray jniMatrix1, jdoubleArray jniMatrix2)
{

jdouble *doubleCMatrix1 = (*env)->GetDoubleArrayElements(env, jniMatrix1, NULL);
jdouble *doubleCMatrix2 = (*env)->GetDoubleArrayElements(env, jniMatrix2, NULL);

jsize length = (*env)->GetArrayLength(env, jniMatrix1);

jdoubleArray finalSum = (*env)->NewDoubleArray(env, length);

jdouble *preparingSum = (*env)->GetDoubleArrayElements(env, finalSum, NULL);    

    int i;
    for(i = 0; i < length; i++) {
    preparingSum[i] = doubleCMatrix1[i] + doubleCMatrix2[i];    
}
(*env)->ReleaseDoubleArrayElements(env, jniMatrix1, doubleCMatrix1, 0);
(*env)->ReleaseDoubleArrayElements(env, jniMatrix2, doubleCMatrix2, 0);
(*env)->ReleaseDoubleArrayElements(env, finalSum, preparingSum, 0);

return finalSum;

}

我的转换都在这里:

 public void convert2To1D(double [][] aMatrix) {

   List<Double> list = new ArrayList<Double>();

    for(int i = 0; i < row2; i++)
        for(int j = 0; j < column2; j++) {
           list.add(aMatrix[i][j]);

        }
        conversion2 = new double[list.size()];
        for(int i = 0; i < conversion2.length; i++)
            conversion2[i] = list.get(i);

}

public void convertTo1D(double [][] aMatrix) {

    List<Double> list = new ArrayList<Double>();

    for(int i = 0; i < row1; i++)
        for(int j = 0; j < column1; j++) {
           list.add(aMatrix[i][j]);

        }
        conversion1 = new double[list.size()];
        for(int i = 0; i < conversion1.length; i++)
            conversion1[i] = list.get(i);


}

我的原生声明:

 public native double[] summationOfMatricesViaC(int rows, int columns, double[] aMatrix1, double[] aMatrix2);

我认为转换可能是错误的并且没有获得所有元素,但我可能是错的。尝试改变各种时间,但仍然崩溃。有什么帮助吗?

编辑:转换没问题!传递NULL参数,这就是为什么它在如此激烈的事情中崩溃。

1 个答案:

答案 0 :(得分:0)

解决方案:傻傻的我,我传递了NULL参数,这导致了错误。记住要始终确保您的参数包含数据!