注意:我使用的是Android Studio,使用Android Studio以外的NDK编译C代码。
以下C代码:
jintArray Java_com_xxx_yyy_MainActivity_processFrame(
JNIEnv* env, jobject thiz, jbyteArray inArray)
{
int i;
jsize inLength = (*env)->GetArrayLength(env, inArray);
int outLength = inLength/3*2; // YYYYUV
jbyte in[outLength];
(*env)->GetByteArrayRegion(env, inArray, 0, outLength, in);
jint out[outLength];
jintArray outArray = (*env)->NewIntArray(env, outLength);
int alpha = 255 << 24;
for (i=0;i<outLength;i++) {
// just use the Y to set the B for now
out[i]=alpha | in[i];
}
// No release needed since using GetXXXRegion
//(*env)->ReleaseByteArrayElements(env, inArray, in, JNI_ABORT);
(*env)->SetIntArrayRegion(env, outArray, 0, outLength, out);
return outArray;
}
从Java调用如下:
System.out.println("Calling processFrame");
int[] rgb = processFrame(imageData);
System.out.println("Call completed");
然而,我确实看到&#34;调用processFrame&#34;在我的控制台上,但没有得到&#34;呼叫已完成。&#34;相反,我得到
07-14 15:41:20.219 25810-25945/com.xxx.yyy A/libc﹕ Fatal signal 11 (SIGSEGV) at 0x5ef78000 (code=2), thread 25945 (CAMERA_THREAD)
07-14 15:41:20.219 25810-25814/com.xxx.yyy D/dalvikvm﹕ GC_CONCURRENT freed 1K, 6% free 8579K/9048K, paused 2ms+1ms, total 13ms
07-14 15:41:20.299 128-415/? W/CameraService﹕ Disconnecting camera client 0x427b09a8 since the binder for it died (this pid 128)
07-14 15:41:20.299 493-784/? I/WindowState﹕ WIN DEATH: Window{413fb7d0 u0 com.xxx.yyy/com.xxx.yyy.MainActivity}
07-14 15:41:20.299 493-493/? I/ActivityManager﹕ Process com.xxx.yyy (pid 25810) has died.
07-14 15:41:20.299 493-784/? W/WindowManager﹕ Force-removing child win Window{41685140 u0 SurfaceView} from container Window{413fb7d0 u0 com.xxx.yyy/com.xxx.yyy.MainActivity}
07-14 15:41:20.299 493-493/? W/ActivityManager﹕ Force removing ActivityRecord{41397308 u0 com.xxx.yyy/.MainActivity}: app died, no saved state
07-14 15:41:20.309 493-493/? D/WindowManager﹕ adjustConfigurationLw, config:{1.0 ?mcc?mnc ?locale ?layoutDir sw800dp w1280dp h752dp 240dpi xlrg land ?uimode ?night finger -keyb/v/h -nav/v} mLidState:-1 mHasDockFeature:true mHasKeyboardFeature:true mHasHallSensorFeature:true config.hardKeyboardHidden:2
07-14 15:41:20.309 493-493/? W/NvCpuClient﹕ Failed to bind to service
我的C代码编写和调用Java的方式是否存在内在错误,可以解释这个错误?
如何从Android Studio调试C代码中发生的错误?
注意:仅使用
替换C函数体return (*env)->NewIntArray(env, 1000);
允许对C进行调用并返回,因此绑定本身有效,这表明错误&#34;无法绑定到服务&#34;就在那里,因为这个过程已经死了,不能再次从Java中再次调用(我在循环中调用这个processFrame)。
更新:偶尔但不总是,我也会得到
Consumer closed input channel or an error occurred. events=0x9
作为错误消息的一部分。我真的不明白为什么,但这似乎与我的问题有关。我不应该从C代码中释放outArray。我应该吗?
更新2:我隔离了问题:错误是由内存分配引起的。代码正确执行outLength = 320 * 480(或任何小于那个),但是当我得到SD(640 * 480)时,我得到了我描述的错误。我提供的C代码中的任何内存分配都不正确吗?有没有更合适的方法从C代码中分配更大的内存?应该使用malloc代替吗?我是否需要增加堆内存,如果需要,怎么做?