JNI中Fatal signal 11 (SIGSEGV), code 1的错误.

这个错,搞了一天才搞定.还是自己对jni不熟.

com.viking.myapplication A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x9c in tid 9502 (g.myapplication).


如果对jni熟悉的话,可能很快就能知道这个错误.但是,如何是刚开始搞jni开发的人来说,可能就一脸懵比了.

这啥玩意啊.

网上搜了很多资料,基本上都是说什么,c代码中存在空指针异常,或者索引越界异常.  反正就是说内存的问题.  

在一次无意中搜到tid这个词,thread ID,线程ID? 根据这个思路, 然后一搜,才发现原来,JNIEnv*env 是一个线程对应一个env,线程间不可以共享同一个env变量。

1.首先C代码添加全局变量

JavaVM * vm;  //创建javavm的全局变量

2.通过env->GetJavaVM(&vm);来获取javavm指针,保存到vm中. env是当前线程的变量.

3.在新线程中:

JNIEnv *env;

vm->AttachCurrentThread(&env,NULL); //通过vm给env赋值

4.这样在子线程中用env,就行了.


代码:

<1.C主线程初始化

jclass jjnimethodclass;
jobject jjnimethodjobject;
JavaVM *g_vm = NULL;
jmethodID jmethod;
jclass jimageuserinfoclazz;
jclass jpicinfoclazz ;
jmethodID jimageuserinfoID;
jmethodID jpicinfoID;
jmethodID jregmethod;
JNIEXPORT jint JNICALL Java_com_viking_myapplication_JniMethod_NetInit
        (JNIEnv *env, jobject jobj){
jjnimethodclass = env->GetObjectClass(jobj);
jjnimethodjobject = (jobject)env->NewGlobalRef(jobj);
env->GetJavaVM(&g_vm);
jclass jimageus = env->FindClass("com/viking/bean/TImageUserInfo");
jclass jpicinfo = env->FindClass("com/viking/bean/TPicInfo");
jimageuserinfoclazz = (jclass)env->NewGlobalRef(jimageus);
jpicinfoclazz = (jclass)env->NewGlobalRef(jpicinfo);
jimageuserinfoID = env->GetMethodID(jimageuserinfoclazz,"<init>","()V");
jpicinfoID = env->GetMethodID(jpicinfoclazz,"<init>","()V");
jregmethod = env->GetMethodID(jjnimethodclass,"NetRegImageRecvExCallBack","(IILcom/viking/bean/TImageUserInfo;Lcom/viking/bean/TPicInfo;)V");
}


<2.回调函数,C辅助线程回调.

int GetImageCbEx(DCHANDLE tHandle, unsigned int  uiImageId,  T_ImageUserInfo *ptImageInfo,T_PicInfo *ptPicInfo,void *pUser){
    LOGE("GetImageCbEx:回调");
    JNIEnv *env2;
    //g_vm->AttachCurrentThread(&env2, NULL);
    bool ret = false;
    if(g_vm != NULL) {
        JavaVMAttachArgs vmAttachArgs;
        vmAttachArgs.version = JNI_VERSION_1_6;
        vmAttachArgs.name = NULL;
        vmAttachArgs.group = NULL;
        jint attachRet = g_vm->AttachCurrentThread(&env2, &vmAttachArgs);
        if(attachRet == 0) {
            ret = true;
        } else{
            LOGE("attachRet != 0");
            ret = false;
        }
    } else{
        LOGE("g_vm == NULL");
        ret = false;
    }
    if (ret){
        //创建两个对象
        jobject jimageuserinfoobj = env2->NewObject(jimageuserinfoclazz,jimageuserinfoID);
        jobject jpicinfoobj = env2->NewObject(jpicinfoclazz,jpicinfoID);
        jfieldID usWidthID = env2->GetFieldID(jimageuserinfoclazz,"usWidth","S");
        env2->SetShortField(jimageuserinfoobj,usWidthID,ptImageInfo->usWidth);
        jfieldID usHeightID = env2->GetFieldID(jimageuserinfoclazz,"usHeight","S");
        env2->SetShortField(jimageuserinfoobj,usHeightID,ptImageInfo->usHeight);
        jfieldID ucVehicleColorID = env2->GetFieldID(jimageuserinfoclazz,"ucVehicleColor","B");
        env2->SetByteField(jimageuserinfoobj,ucVehicleColorID,ptImageInfo->ucVehicleColor);
        jfieldID ucVehicleBrandID = env2->GetFieldID(jimageuserinfoclazz,"ucVehicleBrand","B");
        env2->SetByteField(jimageuserinfoobj,ucVehicleBrandID,ptImageInfo->ucVehicleBrand);
        jfieldID ucVehicleSizeID = env2->GetFieldID(jimageuserinfoclazz,"ucVehicleSize","B");
        env2->SetByteField(jimageuserinfoobj,ucVehicleSizeID,ptImageInfo->ucVehicleSize);
        jfieldID ucPlateColorID = env2->GetFieldID(jimageuserinfoclazz,"ucPlateColor","B");
        env2->SetByteField(jimageuserinfoobj,ucPlateColorID,ptImageInfo->ucPlateColor);
        jfieldID szLprResult16ID = env2->GetFieldID(jimageuserinfoclazz,"szLprResult16","[B");
        jbyteArray szlprresult16arr = env2->NewByteArray(16);
        jbyte *szlp = (jbyte*)(ptImageInfo->szLprResult);
        env2->SetByteArrayRegion(szlprresult16arr,0,16,szlp);
        env2->SetObjectField(jimageuserinfoobj,szLprResult16ID,szlprresult16arr);
        jfieldID usLpBox4ID = env2->GetFieldID(jimageuserinfoclazz,"usLpBox4","[S");
        jshortArray uslpbox4arr = env2->NewShortArray(4);
        jshort* uslp = (jshort*)(ptImageInfo->usLpBox);
        env2->SetShortArrayRegion(uslpbox4arr,0,4,uslp);
        env2->SetObjectField(jimageuserinfoobj,usLpBox4ID,uslpbox4arr);
        jfieldID ucLprTypeID = env2->GetFieldID(jimageuserinfoclazz,"ucLprType","B");
        env2->SetByteField(jimageuserinfoobj,ucLprTypeID,ptImageInfo->ucLprType);
        jfieldID usSpeedID = env2->GetFieldID(jimageuserinfoclazz,"usSpeed","S");
        env2->SetShortField(jimageuserinfoobj,usSpeedID,ptImageInfo->usSpeed);
        jfieldID ucSnapTypeID = env2->GetFieldID(jimageuserinfoclazz,"ucSnapType","B");
        env2->SetByteField(jimageuserinfoobj,ucSnapTypeID,ptImageInfo->ucSnapType);
        jfieldID ucReservedID = env2->GetFieldID(jimageuserinfoclazz,"ucReserved","B");
        env2->SetByteField(jimageuserinfoobj,ucReservedID,ptImageInfo->ucReserved);
        jfieldID acSnapTime18ID = env2->GetFieldID(jimageuserinfoclazz,"acSnapTime18","[B");
        jbyteArray acsnaptime18arr = env2->NewByteArray(18);
        jbyte* acsna = (jbyte*)(ptImageInfo->acSnapTime);
        env2->SetByteArrayRegion(acsnaptime18arr,0,18,acsna);
        env2->SetObjectField(jimageuserinfoobj,acSnapTime18ID,acsnaptime18arr);
        jfieldID ucViolateCodeID = env2->GetFieldID(jimageuserinfoclazz,"ucViolateCode","B");
        env2->SetByteField(jimageuserinfoobj,ucViolateCodeID,ptImageInfo->ucViolateCode);
        jfieldID ucLaneNoID = env2->GetFieldID(jimageuserinfoclazz,"ucLaneNo","B");
        env2->SetByteField(jimageuserinfoobj,ucLaneNoID,ptImageInfo->ucLaneNo);
        jfieldID uiVehicleIdID = env2->GetFieldID(jimageuserinfoclazz,"uiVehicleId","I");
        env2->SetIntField(jimageuserinfoobj,uiVehicleIdID,ptImageInfo->uiVehicleId);
        jfieldID ucScoreID = env2->GetFieldID(jimageuserinfoclazz,"ucScore","B");
        env2->SetByteField(jimageuserinfoobj,ucScoreID,ptImageInfo->ucScore);
        jfieldID ucDirectionID = env2->GetFieldID(jimageuserinfoclazz,"ucDirection","B");
        env2->SetByteField(jimageuserinfoobj,ucDirectionID,ptImageInfo->ucDirection);
        jfieldID ucTotalNumID = env2->GetFieldID(jimageuserinfoclazz,"ucTotalNum","B");
        env2->SetByteField(jimageuserinfoobj,ucTotalNumID,ptImageInfo->ucTotalNum);
        jfieldID ucSnapshotIndexID = env2->GetFieldID(jimageuserinfoclazz,"ucSnapshotIndex","B");
        env2->SetByteField(jimageuserinfoobj,ucSnapshotIndexID,ptImageInfo->ucSnapshotIndex);

        //对象二
        jfieldID uiPanoramaPicLenID = env2->GetFieldID(jpicinfoclazz,"uiPanoramaPicLen","I");
        jint len1 = ptPicInfo->uiPanoramaPicLen;
        env2->SetIntField(jpicinfoobj,uiPanoramaPicLenID,ptPicInfo->uiPanoramaPicLen);
        jfieldID uiVehiclePicLenID = env2->GetFieldID(jpicinfoclazz,"uiVehiclePicLen","I");
        jint len2 = ptPicInfo->uiVehiclePicLen;
        env2->SetIntField(jpicinfoobj,uiVehiclePicLenID,ptPicInfo->uiVehiclePicLen);
        jfieldID ptPanoramaPicBuffID = env2->GetFieldID(jpicinfoclazz,"ptPanoramaPicBuff","[B");
        jbyteArray ptpanoramapicbuffstrarr = env2->NewByteArray(len1);
        jbyte * ptpan = (jbyte*)(ptPicInfo->ptPanoramaPicBuff);
        env2->SetByteArrayRegion(ptpanoramapicbuffstrarr,0,len1,ptpan);
        env2->SetObjectField(jpicinfoobj,ptPanoramaPicBuffID,ptpanoramapicbuffstrarr);
        jfieldID ptVehiclePicBuffID = env2->GetFieldID(jpicinfoclazz,"ptVehiclePicBuff","[B");
        jbyteArray ptvehiclepicbuffarr = env2->NewByteArray(len2);
        jbyte* ptveh = (jbyte*)(ptPicInfo->ptVehiclePicBuff);
        env2->SetByteArrayRegion(ptvehiclepicbuffarr,0,len2,ptveh);
        env2->SetObjectField(jpicinfoobj,ptVehiclePicBuffID,ptvehiclepicbuffarr);
        jstring  puser = env2->NewStringUTF((char*)pUser);
        env2->CallVoidMethod(jjnimethodjobject,jregmethod,tHandle,uiImageId,jimageuserinfoobj,jpicinfoobj);
        g_vm->DetachCurrentThread();
    } else{
        LOGE("GetImageCbEx 回调失败.");
    }
    return 0;
}

<3.注册回调函数
//图片抓拍注册回调接口 1
JNIEXPORT jint JNICALL Java_com_viking_myapplication_JniMethod_NetRegImageRecvEx(JNIEnv *env, jobject jobj, jint tHandle, jstring pUser){
LOGE("Java_com_viking_myapplication_JniMethod_NetRegImageRecvEx");
char *puser = (char*)env->GetStringUTFChars(pUser,0);
return Net_RegImageRecvEx(tHandle, GetImageCbEx,(void*)puser);
}

<4.回调方法

JNIEXPORT jint JNICALL Java_com_viking_myapplication_JniMethod_NetImageSnap
        (JNIEnv *env, jobject jobj, jint tHandle,jobject ptImageSnap){
    LOGE("Java_com_viking_myapplication_JniMethod_NetImageSnap");

    jclass tmp = env->GetObjectClass(jobj);
    jregimageexclass = (jclass)env->NewGlobalRef(tmp);
    jregimageexobject = (jobject)env->NewGlobalRef(jobj);
    T_DCImageSnap t_dcImageSnap;
    jclass jclazz = env->GetObjectClass(ptImageSnap);
    jfieldID uiImageIdID = env->GetFieldID(jclazz,"uiImageId","I");
    t_dcImageSnap.uiImageId = (unsigned int)env->GetIntField(ptImageSnap,uiImageIdID);
    jfieldID ucLightIndexID =env->GetFieldID(jclazz,"ucLightIndex","B");
    t_dcImageSnap.ucLightIndex = (unsigned char)env->GetByteField(ptImageSnap,ucLightIndexID);
    jfieldID ucLightModeID =env->GetFieldID(jclazz,"ucLightMode","B");
    t_dcImageSnap.ucLightMode =(unsigned char)env->GetByteField(ptImageSnap,ucLightModeID);
    jfieldID usGroupIdID =env->GetFieldID(jclazz,"usGroupId","S");
    t_dcImageSnap.usGroupId =(unsigned short)env->GetShortField(ptImageSnap,usGroupIdID);
    jint result =Net_ImageSnap(tHandle,&t_dcImageSnap);
    return result;
}


另外这种报错,也可能是空指针或者索引越界异常.(这一点要注意排除);





阅读更多
换一批

Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1)

12-02

正在调试公司遗留项目,win32平台下弄些按钮点击会崩溃,貌似是某些参数没有初始化rnrn上面的问题暂时没理会,然后移植到Android平台,运行时出现标题中的错误,(cocos2dx3.2)rnrn其他日志如下rnrn[code=c]rn12-02 10:03:25.918: W/System.err(1392): at com.tencent.permissionfw.permission.a.a(SourceFile:272)rn12-02 10:03:25.918: W/System.err(1392): at tcs.awt$a.onTransact(SourceFile:127)rn12-02 10:03:25.918: W/System.err(1392): at android.os.Binder.execTransact(Binder.java:404)rn//---------------------------------------------------------------------rn12-02 10:03:25.918: A/libc(23705): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 23734 (Thread-458)rn12-02 10:03:25.918: W/System.err(1392): at dalvik.system.NativeStart.run(Native Method)rn12-02 10:03:25.928: E/Parcel(794): Reading a NULL string not supported here.rn12-02 10:03:25.978: D/dalvikvm(1339): GC_EXPLICIT freed 2001K, 13% free 16169K/18484K, paused 2ms+7ms, total 40msrn12-02 10:03:26.028: I/DEBUG(172): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***rn12-02 10:03:26.028: I/DEBUG(172): Build fingerprint: 'google/occam/mako:4.4.4/KTU84P/1227136:user/release-keys'rn12-02 10:03:26.028: I/DEBUG(172): Revision: '11'rn12-02 10:03:26.028: I/DEBUG(172): pid: 23705, tid: 23734, name: Thread-458 >>> com.any.iee <<rn12-02 10:03:26.138: I/DEBUG(172): #01 pc 004f36bf /data/app-lib/com.any.iee-2/libcocos2dcpp.so (Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit+234)rn12-02 10:03:26.138: I/DEBUG(172): #02 pc 0001dbcc /system/lib/libdvm.so (dvmPlatformInvoke+112)rn12-02 10:03:26.138: I/DEBUG(172): #03 pc 0004e123 /system/lib/libdvm.so (dvmCallJNIMethod(unsigned int const*, JValue*, Method const*, Thread*)+398)rn12-02 10:03:26.138: I/DEBUG(172): #04 pc 0004fb0d /system/lib/libdvm.so (dvmResolveNativeMethod(unsigned int const*, JValue*, Method const*, Thread*)+184)rn12-02 10:03:26.138: I/DEBUG(172): #05 pc 00026fe0 /system/lib/libdvm.sorn12-02 10:03:26.138: I/DEBUG(172): #06 pc 0002dfa0 /system/lib/libdvm.so (dvmMterpStd(Thread*)+76)rn12-02 10:03:26.138: I/DEBUG(172): #07 pc 0002b638 /system/lib/libdvm.so (dvmInterpret(Thread*, Method const*, JValue*)+184)rn12-02 10:03:26.138: I/DEBUG(172): #08 pc 0006057d /system/lib/libdvm.so (dvmCallMethodV(Thread*, Method const*, Object*, bool, JValue*, std::__va_list)+336)rn12-02 10:03:26.138: I/DEBUG(172): #09 pc 000605a1 /system/lib/libdvm.so (dvmCallMethod(Thread*, Method const*, Object*, JValue*, ...)+20)rn12-02 10:03:26.138: I/DEBUG(172): #10 pc 00055287 /system/lib/libdvm.sorn12-02 10:03:26.138: I/DEBUG(172): #11 pc 0000d170 /system/lib/libc.so (__thread_entry+72)rn12-02 10:03:26.138: I/DEBUG(172): #12 pc 0000d308 /system/lib/libc.so (pthread_create+240)rn12-02 10:03:26.138: I/DEBUG(172): stack:rn12-02 10:03:26.138: I/DEBUG(172): 75a79a30 75a79a20 [stack:23734]rn12-02 10:03:26.138: I/DEBUG(172): 75a79a34 747981c5 /system/lib/egl/libGLESv2_adreno.so (glClearColor+30)rn12-02 10:03:26.138: I/DEBUG(172): 75a79a38 00000021 rn12-02 10:03:26.138: I/DEBUG(172): 75a79a3c 00000018 rnrn[/code]rnrn之后用ndk的ndk-stack工具定位错误位置rn结果是 工程目录下cocos2d\cocos\platform\android中的javaactivity.cpp中的67行错误,以下是javaactivity.cpprn[code=c]rn/****************************************************************************rnCopyright (c) 2013-2014 Chukong Technologies Inc.rnrnhttp://www.cocos2d-x.orgrnrnPermission is hereby granted, free of charge, to any person obtaining a copyrnof this software and associated documentation files (the "Software"), to dealrnin the Software without restriction, including without limitation the rightsrnto use, copy, modify, merge, publish, distribute, sublicense, and/or sellrncopies of the Software, and to permit persons to whom the Software isrnfurnished to do so, subject to the following conditions:rnrnThe above copyright notice and this permission notice shall be included inrnall copies or substantial portions of the Software.rnrnTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORrnIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,rnFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THErnAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERrnLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,rnOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS INrnTHE SOFTWARE.rn****************************************************************************/rnrn#include "base/CCPlatformConfig.h"rn#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROIDrnrn#include "CCApplication.h"rn#include "base/CCDirector.h"rn#include "base/CCEventCustom.h"rn#include "base/CCEventType.h"rn#include "renderer/CCGLProgramCache.h"rn#include "renderer/CCTextureCache.h"rn#include "2d/CCDrawingPrimitives.h"rn#include "CCGLView.h"rn#include "platform/android/jni/JniHelper.h"rn#include rn#include rnrn#define LOG_TAG "main"rn#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)rnrnvoid cocos_android_app_init(JNIEnv* env, jobject thiz) __attribute__((weak));rnrnusing namespace cocos2d;rnrnextern "C"rnrnrnjint JNI_OnLoad(JavaVM *vm, void *reserved)rnrn JniHelper::setJavaVM(vm);rnrn return JNI_VERSION_1_4;rnrnrnvoid Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thiz, jint w, jint h)rnrn auto director = cocos2d::Director::getInstance();rn auto glview = director->getOpenGLView();rn if (!glview)rn rn glview = cocos2d::GLView::create("Android app");rn glview->setFrameSize(w, h);rn director->setOpenGLView(glview);rn rn cocos_android_app_init(env, thiz);//出错位置 rnrn cocos2d::Application::getInstance()->run();rn rn elsern rn cocos2d::GL::invalidateStateCache();rn cocos2d::GLProgramCache::getInstance()->reloadDefaultGLPrograms();rn cocos2d::DrawPrimitives::init();rn cocos2d::VolatileTextureMgr::reloadAllTextures();rnrn cocos2d::EventCustom recreatedEvent(EVENT_RENDERER_RECREATED);rn director->getEventDispatcher()->dispatchEvent(&recreatedEvent);rn director->setGLDefaultValues();rn rnrnrnrnvoid Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnSurfaceChanged(JNIEnv* env, jobject thiz, jint w, jint h)rnrn cocos2d::Application::getInstance()->applicationScreenSizeChanged(w, h);rnrnrnrnrn#endif // CC_TARGET_PLATFORM == CC_PLATFORM_ANDROIDrnrn[/code]rn错误的函数在obj.android/jni/hellocpp中的main.cpp中,以下是main.cpprn[code=c]rn#include "AppDelegate.h"rn#include "cocos2d.h"rn#include "platform/android/jni/JniHelper.h"rn#include rn#include rnrn#define LOG_TAG "main"rn#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)rnrnusing namespace cocos2d;rnrnvoid cocos_android_app_init (JNIEnv* env, jobject thiz) rn LOGD("cocos_android_app_init");rn AppDelegate *pAppDelegate = new AppDelegate();rnrn[/code]rn搞了一周了,没弄出来,快疯了。开始有人标题的错误是资源没找到,但是assets中的资源是eclipse运行时ndk自动复制过来的。还有人说是资源路径大小写没区分,我也检查了一部分,都区分了(量很大,所以只检查了最先调用的几个资源)rnrn求大神指点!!!!!!!!!!!!!!!!!!rn

没有更多推荐了,返回首页