English 中文(简体)
What could cause SIGSEGV when calling NewObjectArray for JNI in Android?
原标题:

I just started working with the Android NDK but I keep getting SIGSEGV when I have this call in my C code:

jobjectArray someStringArray;
someStringArray = (*env)->NewObjectArray(env, 10, 
(*env)->FindClass(env,"java/lang/String"),(*env)->NewStringUTF(env, ""));

Base on all the example I can find, the above code is correct but I keep getting SIGSERGV and everything is ok if the NewObjectArray line is commented out. Any idea what could cause such a problem?

最佳回答

that looks right, so i m guessing you ve done something else wrong. i assume you re running with checkjni on? you might want to break that up into multiple lines: do the FindClass and check the return value, do the NewStringUTF and check the return value, and then call NewObjectArray.

btw, you might want to pass NULL as the final argument; this pattern of using the empty string as the default value for each element of the array is commonly used (i think it s copy & pasted from some Sun documentation and has spread from there) but it s rarely useful, and it s slightly wasteful. (and it doesn t match the behavior of "new String[10]" in Java.)

问题回答

I guess one of the possible causes is that in a long-run JNI method, the VM aborts when running out of the per-method-invocation local reference slots (normally 512 slots in Android).

Since FindClass() and NewStringUTF() functions would allocate local references, if you stay in a JNI method for a long time, the VM do not know whether a specific local reference should be recycled or not. So you should explicitly call DeleteLocalRef() to release the acquired local references when not required anymore. If you don t do this, the "zombie" local references will occupy slots in VM, and the VM aborts while running out of all the local reference slots.

In short-run JNI method, this may not be a problem due to all the local references would be recycled when exiting from a JNI method.





相关问题
Android NDK future extended support for C++

Is there any info about the future of extending the safe C++ header list in the NDK (or maybe some hints to what might be safe to use) ? Or how soon we can expect the next NDK update? Also will there ...

Blocking Dialog from within JNI code

I m writing an app that s basically a wrapper around a 250K JNI. The JNI (a game engine) has APIs like handle_penUp(int x, int y). Sometimes it needs to query the user from inside handle_penUp() (...

Read txt file in res/raw/ within Android NDK

I have a file within my application that I want to have included within the .apk for my android app that is a .txt file. My application is almost entirely written in C through the use of the NDK, ...

File Operations in Android NDK

I am using the Android NDK to make an application primarily in C for performance reasons, but it appears that file operations such as fopen do not work correctly in Android. Whenever I try to use ...

Android - How to enable CheckJni for NDK development?

Can someone please tell me if I m missing something here? I am trying the following commands in shell. $ ./adb shell stop $ ./adb shell setprop dalvik.vm.checkjni true $ ./adb shell start But ...

Android NDK: Autogenerate function declarations?

I am trying to use a pre-existing native C library in my android project.. The library builds just fine with the NDK tools... Now what I ve come to understand is that I cannot just call into the ...

热门标签