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. Problem using android services

Hi,
    I am trying to create an application which has a service running
in the background. But i should also be able to re run the service
when i select option(Here it is a check box) from the fore ground. I
am getting Unable to instantiate service error. I have called the
service in my main activity using

Intent periodicService= new Intent();
        periodicService.setAction("com.myservice.myservice");
context.stopService(periodicService);
        context.startService(periodicService);

where my context is got by using getApplicationContext();

Any help would be welcome.
Thanks and Regards,
Abhijeeth

-- 

2. SOLD -- WTS : Nexus One 2nd

Mau update aja..

Sold to temen kampus.. :)

Thanks mod,, temen2..
Maaf kalo ada japri blom saya balesin yah.. :))

~~~
@leonardusjeff
~~~~~~
ent from DROID
On Jul 6, 2010 3:35 AM, "Jeffry Leonardus" <leonardusjeff.andr...@gmail.com>



Misi mods, temen2..
As subject,,

Mo jual gadget kesayangan,, :))
Kondisi 97% lah,, ada dent kecil di pojok kiri atas sebelah tombol power
(look attachment, yg saya lingkarin merah),,
Fungsi HH normal smua,, lengkap (dos + kelengkapan),, rooted.

Mo dijual harga 5,1jt ,,
nego bolee.. ;D

Bonus buat yg ga nawar :
1 Battery ORI
1 Casemate Vroom
2 pouch merk BOA ( dulu beli 1 harga 300an)
1 silicon case

Japri ke : jeffryleonar...@gmail.com

~~~
@leonardusjeff
~~~~~~
ent from DROID
-- 
"Indonesian Android Community [id-android]" 

3. How to fix: focusable EditText prevents ScrollView from scrolling

4. Android library project custom build

5. Kernel will not load wifi driver

6. Android 2.2 challenges the iPhone 4 to a JavaScript duel鈥?and wins!

7. (SHARE) Lagi2 Force Close