Solved

JNI: modify array content in C++ code

Posted on 2002-05-29
5
3,238 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
  • 2
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
pellep earned 50 total points
Comment Utility
Try
env->ReleaseIntArrayElements(arr, jarr, 0);
instead of
env->ReleaseIntArrayElements(arr, jarr, JNI_ABORT);
0
 
LVL 3

Expert Comment

by:msterjev
Comment Utility
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
Comment Utility
wasn't that exactly what I said?
0
 
LVL 3

Expert Comment

by:msterjev
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now