Re: Can't allocate large char array in JNI
On Mon, 21 Apr 2008 08:07:08 -0700 (PDT), CD1 wrote:
The jchar *value comes from the object allocated by the library
OpenCV, ant its size is the imageSize field of the IplImage struct.
What is the real declared type of the cv_image->imageData, i.e. before
the explicit typecast?
What are "sizeof(jchar)" and "sizeof(cv_image->imageData[0])"?
Here's something you can test, add this to the start of set_char_array()
before calling any of the jni functions:
volatile jchar tmp;
int i;
for (i=0; i<length; i++) {
tmp = value[i];
}
Does this code pass ok?
Try a small example without using your image library. With the
following code I can successfully allocate char arrays of 30MB using
the default java heap size. Beyond that, ExceptionDescribe() correctly
identifies the error as "Out of memory: Java heap space":
// public static native char[] getCharArray(int n);
JNIEXPORT jcharArray JNICALL
Java_Chars_getCharArray(JNIEnv *env, jclass unused, jint n)
{
jcharArray jca;
jchar *jc;
int i;
jca = (*env)->NewCharArray(env, n);
if ((*env)->ExceptionOccurred(env)) {
(*env)->ExceptionDescribe(env);
}
if ((jc = malloc(n * sizeof(jchar)))) {
// some nonsense initial values
for (i=0; i<n; i++) {
jc[i] = i % 255;
}
(*env)->SetCharArrayRegion(env, jca, 0, n, jc);
if ((*env)->ExceptionOccurred(env)) {
(*env)->ExceptionDescribe(env);
}
free(jc);
}
return jca;
}
/gordon
--