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. Possible to check .apk signature?

 Until now, I haven't really concerned myself with app piracy; I only 
have one app under my belt for Android, it's in beta and it's free. I'm 
about to start development on another app and looking to use ProGuard 
for obfuscation and LVL as I would like to make it a paid app, and I've 
been wondering something ever since the news that LVL had been 
circumvented months ago. Is it possible to check the .apk's signature 
from within the API? If not currently, perhaps Google might add some 
code to Android itself or to the API to enable LVL to make sure that the 
.apk signature matches the signature that Android Market already has on 
file?


Just a thought, not even necessarily a feasible one.
Raymond

--

2. What is the URL for an app on android market?

Trying to figure out working URL for my app on Android Market.
Used tutorial from "http://developer.android.com/intl/zh-TW/guide/
publishing/publishing.html" nothing works.
For http:// getting 404 and for market:// getting "to many
redirectiones"

My app id is com.softakgames.tappuzzle, for both links I am getting
http://market.android.com/details?id=com.softakgames.tappuzzle  and
http://market.android.com/search?q=com.softakgames.tappuzzle

404 Error (Not Found)

-- 

3. bugreport tutorial

4. Wierd memory leak

5. GLSurface doesn't draw anything when i came back again.

6. reliable app store?

7. Positions: Android Developers (Mobile)--Great Company! Please read on....