Link to home
Start Free TrialLog in
Avatar of pigpig
pigpig

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of pellep
pellep
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of msterjev
msterjev

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!
wasn't that exactly what I said?
Sorry,pellep!
This site is not multithreading synchronized :-))))).
When I started writing the answer there are no comments there. He can decide about points!
Avatar of pigpig

ASKER

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~~