?
Solved

JNI: modify array content in C++ code

Posted on 2002-05-29
5
Medium Priority
?
3,354 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:pigpig
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
pellep earned 200 total points
ID: 7041586
Try
env->ReleaseIntArrayElements(arr, jarr, 0);
instead of
env->ReleaseIntArrayElements(arr, jarr, JNI_ABORT);
0
 
LVL 3

Expert Comment

by:msterjev
ID: 7041615
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
 
LVL 4

Expert Comment

by:pellep
ID: 7041644
wasn't that exactly what I said?
0
 
LVL 3

Expert Comment

by:msterjev
ID: 7042975
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
 

Author Comment

by:pigpig
ID: 7043563
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month8 days, 22 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question