Pass Array from Java to C (over JNI) work in an activity but fail in a thread?

by nemo » Mon, 11 Jan 2010 07:59:04 GMT


Sponsored Links
 hi! all:

I found that I need to use dynamically allocate for array.
Then the crash problem is solved.

However, I still don't know why it works when the native function is
called in the main activity...

Thanks in advance.




>  return (*env)->NewStringUTF(env, "show when no crash!");}



Pass Array from Java to C (over JNI) work in an activity but fail in a thread?

by Mahendra » Mon, 11 Jan 2010 12:29:32 GMT


 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






>  return (*env)->NewStringUTF(env, "show when no crash!");}


Sponsored Links


Pass Array from Java to C (over JNI) work in an activity but fail in a thread?

by monocasa » Tue, 12 Jan 2010 02:45:38 GMT


 Hey,
  I'm not sure if this is your problem, but you're allocating a really
big chunk of memory on the stack (it makes my embedded code senses
tingle big time).  What are the stack size limits for android?

Tristan Miller



> }



Pass Array from Java to C (over JNI) work in an activity but fail in a thread?

by nemo » Tue, 12 Jan 2010 11:06:29 GMT


 Hi! Mahendra G , Tristan:

Thanks for your information.
I'll try the example. :)

ya. It's a really big chunk of memory on the stack.
Maybe it works in an activity is just a luck....






> - -



Pass Array from Java to C (over JNI) work in an activity but fail in a thread?

by fadden » Wed, 13 Jan 2010 06:37:34 GMT


 


Thread stacks are 1MB.  You can look at /proc/maps (or use "showmap")
to see the segments.  (May require a rooted phone.)

I don't know offhand if the main thread is treated differently, but
that might explain why it works in one context but not another.



Pass Array from Java to C (over JNI) work in an activity but fail in a thread?

by nemo » Mon, 18 Jan 2010 15:39:52 GMT


 fadden:

Thanks for the information!





> that might explain why it works in one context but not another.



Other Threads

1. touch hanging

Hello,
  This may not be the right avenue, but I'm not sure what is.
  The touch functionality on my screen will often stop working (ie the phone
doesn't recognize touches, but the keypad/buttons still work fine).  The
only way to fix this, afaik, is a reboot, which is annoying to do several
times a day.  Is there a way to fix the issue itself, or somehow fix it
without restarting the phone (ie is their a process I can kill or
something)?

Thanks

--~--~---------~--~----~------------~-------~--~----~

2. audio recording to split (small) files without dropping or overlapping samples

we are implementing a sound level meter. For privacy reasons, we don't want
the audio lying around on the disk.

We could do it on the fly without recording to disk, however I don't think
that is possible with the sdk ... is it ?

Matt








-- 
http://www.flatmaxstudios.com

--~--~---------~--~----~------------~-------~--~----~

3. cannot build android x86 on Mac

4. Porting issue- sdk1.0 in omap2430

5. Media Player surprises me!

6. Launching Multiple Activities?

7. XML example of ExpandableListView