I’m implementing a Java method as Native in C++
JNIEXPORT jobjectArray JNICALL myMethod(JNIEnv *env, jclass, //parameters){
int** result = //my function to obtain a matrix n x m of integers
std::vector<jint> tmp;
//fill the vector tmp with matrix integers and deallocate the matrix
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
tmp[m*i + j] = result[i][j];
}
free(result[i]);
}
free(result);
jintArray jResults = env->NewIntArray( tmp.size() );
env->SetIntArrayRegion( jResults, 0, tmp.size(), &tmp[0] );
return env->NewObjectArray(tmp.size(), env->GetObjectClass(jResults), 0);
}
It compiles fine and java loads the dll successfully. But i get this error at runtime:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x10009a7d, pid=2264, tid=3856
#
# JRE version: 7.0-b147
# Java VM: Java HotSpot(TM) Client VM (21.0-b17 mixed mode, sharing windows-x86 )
# Problematic frame:
# C [myDLL.dll+0x9a7d]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as: hs_err_pid2264.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.sun.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
Java result 1
Any idea?
Your current code as I’m writing this:
In this code you’re accessing various elements of a zero-size vector
tmp.A
std::vectoris not an associative array; it’s an ordinary contiguous array. Hence you need to either size it up front, or use thepush_backmethod to add new elements. You can specify a size like this:Cheers & hth.,