Hi,
Please refer below example.
java part:
class IntArray {
private native int sumArray(int[] arr);
public static void main(String[] args) {
IntArray p = new IntArray();
int arr[] = new int[10];
for (int i = 0; i < 10; i++) {
arr[i] = i;
}
int sum = p.sumArray(arr);
System.out.println("sum = " + sum);
}
static {
System.loadLibrary("IntArray");
}
}
C part:
JNIEXPORT jint JNICALL
Java_IntArray_sumArray(JNIEnv *env, jobject obj, jintArray arr)
{
jint *carr;
jint i, sum = 0;
carr = (*env)->GetIntArrayElements(env, arr, NULL);
if (carr == NULL) {
return 0; /* exception occurred */
}
for (i=0; i<10; i++) {
sum += carr[i];
}
(*env)->ReleaseIntArrayElements(env, arr, carr, 0);
return sum;
}
Regards,
Mahendra G
On Jan 10, 6:42 pm, nemo <[email protected]> wrote:
> Hi! All:
>
> I've stuggle of this problem few days, search many hours but not sure
> what's I should try.
> Thanks in advance.
> I have a native c function which take jintarray as a parameter, and I
> wanna pass a int array to it.
> I use GetIntArrayRegion to copy out the content of jintArray.
>
> JNIEXPORT jstring Java_com_google_client_DecodeThread_test( JNIEnv*
> env,
> jobject this, jintArray
> verts1 )
> {
>
> jint buf[307200];
> (*env)->GetIntArrayRegion(env, verts1, 0, 307200, buf);
>
> }
>
> It seems work when I call this native function in an activity. (it
> didn't crash ,at least)
> But it always crash when I call this native function in an thread
> which will be called in an activity.
>
> I can't figure out why is that...
> I saw many example, they just pass jintarray...
>
> I also found that If I copy small part of the jintarray, it won't
> crash.
>
> Is there any memory limit of thread ?
> or something related to GC and invalid array pointer?
>
> The following is part of my code.
> Thanks a lot for your help.
>
> in java:
> public native int testmem(int[] img);
>
> private void decode_c()
> {
> ...
> System.loadLibrary("mydecoder");
> int[] img_int = new int[307200];
> int res= test(img_int ); ----------> it will crash here.I can't even
> find out the error, the application closed.
> ...
>
> }
>
> in native c:
>
> JNIEXPORT jstring Java_com_google_client_DecodeThread_test( JNIEnv*
> env,
> jobject this, jintArray
> verts1 )
> {
>
> jint buf[307200];
> (*env)->GetIntArrayRegion(env, verts1, 0, 307200, buf);
> return (*env)->NewStringUTF(env, "show when no crash!");}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en