android O 32位系统报错:JNI DETECTED ERROR IN APPLICATION: use of deleted local reference

本文主要是介绍android O 32位系统报错:JNI DETECTED ERROR IN APPLICATION: use of deleted local reference,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、同样的程序我在android O 64位系统上跑没有任何问题,但是在32位系统上跑就报这个错误
问题原因是:
自己写的类中有这个构造函数:
public AnWBT_BLE_Service(int service_type, long mostSiguuid, long leastSiguuid, int handle, int end_group_handle, ArrayList<AnWBT_BLE_Characteristic> characteristic) {
    this.service_type = service_type;
    this.mostSiguuid = mostSiguuid;
    this.leastSiguuid = leastSiguuid;
    this.handle = handle;
    this.end_group_handle = end_group_handle;
    this.characteristic = characteristic;
}
然后再JNI调用这个构造函数:
jmethodID service_init = env->GetMethodID(service_cls,"<init>","(IJJIILjava/util/ArrayList;)V");
这种调用方式就会有问题。
二、修改方法:使用没有参数的构造函数,然后使用set方法来设置类里面的成员变量
jclass service_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Service");
jmethodID service_init = env->GetMethodID(service_cls,"<init>","()V");
jmethodID service_setService_type = env->GetMethodID(service_cls,"setService_type","(I)V");
jmethodID service_setMostSiguuid = env->GetMethodID(service_cls,"setMostSiguuid","(J)V");
jmethodID service_setLeastSiguuid = env->GetMethodID(service_cls,"setLeastSiguuid","(J)V");
jmethodID service_setHandle = env->GetMethodID(service_cls,"setHandle","(I)V");
jmethodID service_setEnd_group_handle = env->GetMethodID(service_cls,"setEnd_group_handle","(I)V");
jmethodID service_setCharacteristic = env->GetMethodID(service_cls,"setCharacteristic","(Ljava/util/ArrayList;)V");

三、AnWBT_BLE_Service类的代码

package com.anwsdk.service;import android.os.Parcel;
import android.os.Parcelable;import java.util.ArrayList;
import java.util.List;public class AnWBT_BLE_Service implements Parcelable {private int service_type;private long mostSiguuid;private long leastSiguuid;private int handle;private int end_group_handle;private ArrayList<AnWBT_BLE_Characteristic> characteristic;public AnWBT_BLE_Service() {}public AnWBT_BLE_Service(int service_type, long mostSiguuid, long leastSiguuid, int handle, int end_group_handle, ArrayList<AnWBT_BLE_Characteristic> characteristic) {this.service_type = service_type;this.mostSiguuid = mostSiguuid;this.leastSiguuid = leastSiguuid;this.handle = handle;this.end_group_handle = end_group_handle;this.characteristic = characteristic;}protected AnWBT_BLE_Service(Parcel in) {service_type = in.readInt();mostSiguuid = in.readLong();leastSiguuid = in.readLong();handle = in.readInt();end_group_handle = in.readInt();ArrayList<AnWBT_BLE_Characteristic> characteristic_new = new ArrayList<AnWBT_BLE_Characteristic>();in.readTypedList(characteristic_new,AnWBT_BLE_Characteristic.CREATOR);characteristic = characteristic_new;}public int getService_type() {return service_type;}public long getMostSiguuid() {return mostSiguuid;}public long getLeastSiguuid() {return leastSiguuid;}public int getHandle() {return handle;}public int getEnd_group_handle() {return end_group_handle;}public ArrayList<AnWBT_BLE_Characteristic> getCharacteristic() {return characteristic;}public void setService_type(int service_type) {this.service_type = service_type;}public void setMostSiguuid(long mostSiguuid) {this.mostSiguuid = mostSiguuid;}public void setLeastSiguuid(long leastSiguuid) {this.leastSiguuid = leastSiguuid;}public void setHandle(int handle) {this.handle = handle;}public void setEnd_group_handle(int end_group_handle) {this.end_group_handle = end_group_handle;}public void setCharacteristic(ArrayList<AnWBT_BLE_Characteristic> characteristic) {this.characteristic = characteristic;}@Overridepublic int describeContents() {return 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {dest.writeInt(service_type);dest.writeLong(mostSiguuid);dest.writeLong(leastSiguuid);dest.writeInt(handle);dest.writeInt(end_group_handle);dest.writeTypedList(characteristic);}public static final Creator<AnWBT_BLE_Service> CREATOR = new Creator<AnWBT_BLE_Service>() {@Overridepublic AnWBT_BLE_Service createFromParcel(Parcel in) {return new AnWBT_BLE_Service(in);}@Overridepublic AnWBT_BLE_Service[] newArray(int size) {return new AnWBT_BLE_Service[size];}};
}

四、JNI层修改前代码

/*
* Class : com_anwsdk_service
* Method :GATT_GetService()
* Signature : (J)Ljava/util/List;
*/
JNIEXPORT jobject JNICALL Java_com_anwsdk_service_anwbtservice_GATT_GetService(JNIEnv *env, jobject,jlong gatt)
{ANW_LOGI("+++ GATT_GetService---");int i = 0;GATT_HANDLE gattHandle = (GATT_HANDLE)gatt;jclass arraylist_cls = env->FindClass("java/util/ArrayList");jmethodID arraylist_init = env->GetMethodID(arraylist_cls,"<init>","()V");	jmethodID arraylist_add = env->GetMethodID(arraylist_cls, "add", "(Ljava/lang/Object;)Z");jobject service_arraylist = env->NewObject(arraylist_cls,arraylist_init,""); if(gattHandle == NULL){return service_arraylist;}AnW_BLE_Device device;GetBLEDeviceManagerByGATTHandle(&device,gattHandle);if(device.gatt_handle == NULL || device.download_status != BLE_DOWNLOAD_STATUS_COMPLETE){return service_arraylist;}jclass descriptor_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Descriptor");jmethodID descriptor_init = env->GetMethodID(descriptor_cls,"<init>","(IJJ[B)V"); jclass characteristic_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Characteristic");jmethodID characteristic_init = env->GetMethodID(characteristic_cls,"<init>","(IIIJJ[BLjava/util/ArrayList;)V");  jclass service_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Service");jmethodID service_init = env->GetMethodID(service_cls,"<init>","(IJJIILjava/util/ArrayList;)V");for(i = 0;i < device.service_count;i++){jobject characteristic_arraylist = env->NewObject(arraylist_cls,arraylist_init,""); AnW_BLE_Service *service = &(device.ble_service[i]);int j = 0;for(j = 0;j < service->characteristic_count;j++){jobject descriptor_arraylist = env->NewObject(arraylist_cls,arraylist_init,"");AnW_BLE_Service_Info_Characteristic *characteristic = &(service->characteristic[j]);jbyteArray characteristic_value = charToByteArray(env, (const char*)characteristic->value,characteristic->value_len);			int k = 0;for(k = 0;k < characteristic->descriptor_count;k++){AnW_BLE_Service_Info_Characteristic_Descriptor *descriptor = &(characteristic->descriptor[k]);jbyteArray descriptor_value = charToByteArray(env, (const char*)descriptor->value,descriptor->value_len);jobject descriptor_obj = env->NewObject(descriptor_cls,descriptor_init,descriptor->handle,*(long *)(descriptor->uuid + 8),*(long *)(descriptor->uuid),descriptor_value); env->CallBooleanMethod(descriptor_arraylist, arraylist_add, descriptor_obj);}jobject characteristic_obj = env->NewObject(characteristic_cls,characteristic_init,characteristic->handle,characteristic->properties,characteristic->value_handle,*(long *)(characteristic->uuid + 8),*(long *)(characteristic->uuid),characteristic_value,descriptor_arraylist); env->CallBooleanMethod(characteristic_arraylist, arraylist_add, characteristic_obj);}jobject service_obj = env->NewObject(service_cls,service_init,service->service_type,*(long *)(service->uuid + 8),*(long *)(service->uuid),service->handle,service->end_group_handle,characteristic_arraylist); env->CallBooleanMethod(service_arraylist, arraylist_add, service_obj);}ANW_LOGI("--- GATT_GetService ---");return service_arraylist;
}

JNI层修改后代码(这里面其实还修改了一个long类型)

jlong uuid_lsb(u_int8* uuid) {jlong lsb = 0;for (int i = 7; i >= 0; i--) {lsb <<= 8;lsb |= uuid[i];}return lsb;
}jlong uuid_msb(u_int8* uuid) {jlong msb = 0;for (int i = 15; i >= 8; i--) {msb <<= 8;msb |= uuid[i];}return msb;
}
/*
* Class : com_anwsdk_service
* Method :GATT_GetService()
* Signature : (J)Ljava/util/List;
*/
JNIEXPORT jobject JNICALL Java_com_anwsdk_service_anwbtservice_GATT_GetService(JNIEnv *env, jobject,jlong gatt){ANW_LOGI("+++ GATT_GetService---(0x%llx)",gatt);int i = 0;GATT_HANDLE gattHandle = (GATT_HANDLE)gatt;jclass arraylist_cls = env->FindClass("java/util/ArrayList");jmethodID arraylist_init = env->GetMethodID(arraylist_cls,"<init>","()V");	jmethodID arraylist_add = env->GetMethodID(arraylist_cls, "add", "(Ljava/lang/Object;)Z");jobject service_arraylist = env->NewObject(arraylist_cls,arraylist_init,""); if(gattHandle == NULL){return service_arraylist;}AnW_BLE_Device device;GetBLEDeviceManagerByGATTHandle(&device,gattHandle);if(device.gatt_handle == NULL || device.download_status != BLE_DOWNLOAD_STATUS_COMPLETE){return service_arraylist;}jclass descriptor_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Descriptor");jmethodID descriptor_init = env->GetMethodID(descriptor_cls,"<init>","()V"); jmethodID descriptor_setHandle = env->GetMethodID(descriptor_cls,"setHandle","(I)V");jmethodID descriptor_setMostSiguuid = env->GetMethodID(descriptor_cls,"setMostSiguuid","(J)V");jmethodID descriptor_setLeastSiguuid = env->GetMethodID(descriptor_cls,"setLeastSiguuid","(J)V");jmethodID descriptor_setValue = env->GetMethodID(descriptor_cls,"setValue","([B)V");jclass characteristic_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Characteristic");jmethodID characteristic_init = env->GetMethodID(characteristic_cls,"<init>","()V");jmethodID characteristic_setHandle = env->GetMethodID(characteristic_cls,"setHandle","(I)V");jmethodID characteristic_setProperties = env->GetMethodID(characteristic_cls,"setProperties","(I)V");jmethodID characteristic_setValue_handle = env->GetMethodID(characteristic_cls,"setValue_handle","(I)V");jmethodID characteristic_setMostSiguuid = env->GetMethodID(characteristic_cls,"setMostSiguuid","(J)V");jmethodID characteristic_setLeastSiguuid = env->GetMethodID(characteristic_cls,"setLeastSiguuid","(J)V");jmethodID characteristic_setValue = env->GetMethodID(characteristic_cls,"setValue","([B)V");jmethodID characteristic_setDescriptor = env->GetMethodID(characteristic_cls,"setDescriptor","(Ljava/util/ArrayList;)V");jclass service_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Service");jmethodID service_init = env->GetMethodID(service_cls,"<init>","()V");jmethodID service_setService_type = env->GetMethodID(service_cls,"setService_type","(I)V");jmethodID service_setMostSiguuid = env->GetMethodID(service_cls,"setMostSiguuid","(J)V");jmethodID service_setLeastSiguuid = env->GetMethodID(service_cls,"setLeastSiguuid","(J)V");jmethodID service_setHandle = env->GetMethodID(service_cls,"setHandle","(I)V");jmethodID service_setEnd_group_handle = env->GetMethodID(service_cls,"setEnd_group_handle","(I)V");jmethodID service_setCharacteristic = env->GetMethodID(service_cls,"setCharacteristic","(Ljava/util/ArrayList;)V");for(i = 0;i < device.service_count;i++){jobject characteristic_arraylist = env->NewObject(arraylist_cls,arraylist_init,""); AnW_BLE_Service *service = device.ble_service + i;int j = 0;for(j = 0;j < service->characteristic_count;j++){jobject descriptor_arraylist = env->NewObject(arraylist_cls,arraylist_init,"");AnW_BLE_Service_Info_Characteristic *characteristic = &(service->characteristic[j]);jbyteArray characteristic_value = charToByteArray(env, (const char*)characteristic->value,characteristic->value_len);			int k = 0;for(k = 0;k < characteristic->descriptor_count;k++){AnW_BLE_Service_Info_Characteristic_Descriptor *descriptor = &(characteristic->descriptor[k]);jbyteArray descriptor_value = charToByteArray(env, (const char*)descriptor->value,descriptor->value_len);jobject descriptor_obj = env->NewObject(descriptor_cls,descriptor_init,""); env->CallVoidMethod(descriptor_obj,descriptor_setHandle,descriptor->handle);env->CallVoidMethod(descriptor_obj,descriptor_setMostSiguuid,uuid_msb(descriptor->uuid));env->CallVoidMethod(descriptor_obj,descriptor_setLeastSiguuid,uuid_lsb(descriptor->uuid));env->CallVoidMethod(descriptor_obj,descriptor_setValue,NULL);env->CallBooleanMethod(descriptor_arraylist, arraylist_add, descriptor_obj);}jobject characteristic_obj = env->NewObject(characteristic_cls,characteristic_init,""); env->CallVoidMethod(characteristic_obj,characteristic_setHandle,characteristic->handle);env->CallVoidMethod(characteristic_obj,characteristic_setProperties,characteristic->properties);env->CallVoidMethod(characteristic_obj,characteristic_setValue_handle,characteristic->value_handle);env->CallVoidMethod(characteristic_obj,characteristic_setMostSiguuid,uuid_msb(characteristic->uuid));env->CallVoidMethod(characteristic_obj,characteristic_setLeastSiguuid,uuid_lsb(characteristic->uuid));env->CallVoidMethod(characteristic_obj,characteristic_setValue,NULL);env->CallVoidMethod(characteristic_obj,characteristic_setDescriptor,descriptor_arraylist);env->CallBooleanMethod(characteristic_arraylist, arraylist_add, characteristic_obj);}jobject service_obj = env->NewObject(service_cls,service_init,"");env->CallVoidMethod(service_obj,service_setService_type,service->service_type);env->CallVoidMethod(service_obj,service_setMostSiguuid,uuid_msb(service->uuid));env->CallVoidMethod(service_obj,service_setLeastSiguuid,uuid_lsb(service->uuid));env->CallVoidMethod(service_obj,service_setHandle,service->handle);env->CallVoidMethod(service_obj,service_setEnd_group_handle,service->end_group_handle);env->CallVoidMethod(service_obj,service_setCharacteristic,characteristic_arraylist);env->CallBooleanMethod(service_arraylist, arraylist_add, service_obj);}ANW_LOGI("--- GATT_GetService ---");return service_arraylist;
}

这篇关于android O 32位系统报错:JNI DETECTED ERROR IN APPLICATION: use of deleted local reference的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/699884

相关文章

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

Linux系统性能检测命令详解

《Linux系统性能检测命令详解》本文介绍了Linux系统常用的监控命令(如top、vmstat、iostat、htop等)及其参数功能,涵盖进程状态、内存使用、磁盘I/O、系统负载等多维度资源监控,... 目录toppsuptimevmstatIOStatiotopslabtophtopdstatnmon

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

linux重启命令有哪些? 7个实用的Linux系统重启命令汇总

《linux重启命令有哪些?7个实用的Linux系统重启命令汇总》Linux系统提供了多种重启命令,常用的包括shutdown-r、reboot、init6等,不同命令适用于不同场景,本文将详细... 在管理和维护 linux 服务器时,完成系统更新、故障排查或日常维护后,重启系统往往是必不可少的步骤。本文

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

Mac系统下卸载JAVA和JDK的步骤

《Mac系统下卸载JAVA和JDK的步骤》JDK是Java语言的软件开发工具包,它提供了开发和运行Java应用程序所需的工具、库和资源,:本文主要介绍Mac系统下卸载JAVA和JDK的相关资料,需... 目录1. 卸载系统自带的 Java 版本检查当前 Java 版本通过命令卸载系统 Java2. 卸载自定

qt5cored.dll报错怎么解决? 电脑qt5cored.dll文件丢失修复技巧

《qt5cored.dll报错怎么解决?电脑qt5cored.dll文件丢失修复技巧》在进行软件安装或运行程序时,有时会遇到由于找不到qt5core.dll,无法继续执行代码,这个问题可能是由于该文... 遇到qt5cored.dll文件错误时,可能会导致基于 Qt 开发的应用程序无法正常运行或启动。这种错

基于Python实现一个简单的题库与在线考试系统

《基于Python实现一个简单的题库与在线考试系统》在当今信息化教育时代,在线学习与考试系统已成为教育技术领域的重要组成部分,本文就来介绍一下如何使用Python和PyQt5框架开发一个名为白泽题库系... 目录概述功能特点界面展示系统架构设计类结构图Excel题库填写格式模板题库题目填写格式表核心数据结构