JNI: modify array content in C++ code

Dear Experts,

A piece of my C code is as follows:
JNIEXPORT void JNICALL
Java_ABC_fillBuffer(JNIEnv *env, jobject obj, jintArray arr, jint size)
{
jboolean isCopy = JNI_FALSE;
jint *jarr = env->GetIntArrayElements(arr, &isCopy);
for (int i=0; i<size; i++)
   jarr[i] = i;
env->ReleaseIntArrayElements(arr, jarr, JNI_ABORT);
}

When I passed a array to this function in Java code, I noticed that the contents in the array don't change.
What wrong is the code?

My Java code is:
int []arr = new int[40];
XXX.fillBuffer(arr, 40);

Thank you~~

pigpig
pigpigAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
pellepConnect With a Mentor Commented:
Try
env->ReleaseIntArrayElements(arr, jarr, 0);
instead of
env->ReleaseIntArrayElements(arr, jarr, JNI_ABORT);
0
 
msterjevCommented:
The JNI specification says:

0-copy back the content and free the elems buffer
JNI_COMMIT-copy back the content but do not free the elems
buffer
JNI_ABORT- free the buffer without copying back the possible
changes

So the array must be released with:

env->ReleaseIntArrayElements(arr, jarr, 0);

So, use 0 instead of JNI_ABORT!
0
 
pellepCommented:
wasn't that exactly what I said?
0
 
msterjevCommented:
Sorry,pellep!
This site is not multithreading synchronized :-))))).
When I started writing the answer there are no comments there. He can decide about points!
0
 
pigpigAuthor Commented:
Thanks to both pellep and msterjev. My problem is solved.
I really want to award you both but the system doesn't allow me to do so. Thank so much~~
0
All Courses

From novice to tech pro — start learning today.