当前位置:网站首页>Troubleshooting and resolution of program operation problems: an instance of 'std:: Logic_ error‘what(): basic_ string::_ M_ construct null not valid
Troubleshooting and resolution of program operation problems: an instance of 'std:: Logic_ error‘what(): basic_ string::_ M_ construct null not valid
2022-07-27 21:21:00 【nanke_ yh】
Report errors :
terminate called after throwing an instance of 'std::logic_error'what(): basic_string::_M_construct null not valid
java Program call jni Interface running service , Access will crash ,idea Report a mistake as above , There is no response in the browser , And it is a cross domain error ( This is caused by the failure to request a service stop ). Many attempts failed , Check the problems one by one .
Check java Programs and interfaces , There is no mistake , Then check jni Layer code , Note out jni call c++ Layer interface , Found that there are still problems , Then the problem should be jni layer . Further step-by-step investigation found that jstring Problems in the parameter parsing process .
solve the problem :
Because of the need to , Into jstring Parameters , Parameters can be empty and have data content , Parameter in jni Layer resolution to string Pass in C Code , stay C Judgment processing in code . The parameter designed here is null, and the incoming form can be info=null; perhaps info=“”; For this reason, it is passed in “” After that, the program cannot run because of the above error .
jni Used in layers jstring2string Function is :
string jstring2string(JNIEnv *env, jstring jstr)// if jstr by “” Times wrong
{
char* rtn = NULL;
jclass clsstring = env->FindClass("java/lang/String");
jstring strencode = env->NewStringUTF("GB2312");
jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B");
jbyteArray barr = (jbyteArray) env->CallObjectMethod(jstr, mid, strencode);
jsize alen = env->GetArrayLength(barr);
jbyte* ba = env->GetByteArrayElements(barr, JNI_FALSE);
if(alen>0)
{
rtn = (char*)malloc(alen + 1);
memcpy(rtn, ba, alen);
rtn[alen] = 0;
}
env->ReleaseByteArrayElements(barr,ba,0);
string stemp(rtn);
free(rtn);
return stemp;
}It is not difficult to find the reason : Called jstring2string function , Pass in null value "",alen=0, Unprocessed but directly released the related memory , Bad memory is also returned , It will lead to abnormal memory error .
Need to take into account jstr=“” The situation of , Modification code has :
string jstring2string(JNIEnv *env, jstring jstr)
{
char* rtn = NULL;
jclass clsstring = env->FindClass("java/lang/String");
jstring strencode = env->NewStringUTF("GB2312");
jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B");
jbyteArray barr = (jbyteArray) env->CallObjectMethod(jstr, mid, strencode);
jsize alen = env->GetArrayLength(barr);
jbyte* ba = env->GetByteArrayElements(barr, JNI_FALSE);
if(alen>0)
{
rtn = (char*)malloc(alen + 1);
memcpy(rtn, ba, alen);
rtn[alen] = 0;
}
else
{
env->ReleaseByteArrayElements(barr,ba,0);
return "";
}
env->ReleaseByteArrayElements(barr,ba,0);
string stemp(rtn);
free(rtn);
return stemp;
}After modification, the program test can run normally without error , Then the problem is solved .
边栏推荐
- PG free space map & visibility map
- 中英文说明书丨人甲胎蛋白(AFP)ELISA定量试剂盒
- PHP代码审计6—文件包含漏洞
- Multi person collaborative development specification
- Summary of common methods and attributes of arrays and strings in JS
- R language uses LROC function of epidisplay package to visualize ROC curve of logistic regression model and output diagnostic table, visualize multiple ROC curves, and use legend function to add legen
- MySQL data recovery process is based on binlog redolog undo
- 数字化工厂管理系统有哪些价值
- 行为级描述与RTL级描述
- PG 之 Free Space Map & Visibility Map
猜你喜欢

Automated testing - unittest framework

ECCV 2022 | 中科大&京东提出:数据高效的Transformer目标检测器

“地理-语言”大模型文心ERNIE-GeoL及应用

Digital leading planning first, focusing on the construction of intelligent planning information platform and the exploration and practice of application projects

Knife4j dynamically refreshes global parameters through JS

What if the start button doesn't respond after the win11 system updates kb5014668?

LeetCode每日一练 —— 206. 反转链表

中英文说明书丨人甲胎蛋白(AFP)ELISA定量试剂盒

JS closure knowledge

图解 SQL,这也太形象了吧!
随机推荐
QT link MSSQL
30分钟彻底弄懂 synchronized 锁升级过程
Dobot Magician 机器臂-简介
Leetcode-209- subarray with the smallest length
工程技术开发的圈套与局限性
PG 之 Free Space Map & Visibility Map
LeetCode每日一练 —— 21. 合并两个有序链表
ECCV 2022 | 中科大&京东提出:数据高效的Transformer目标检测器
Read Plato & nbsp; Eplato of farm and the reasons for its high premium
Command line PDF Converter::: fcoder 2PDF
Guava Cache 原理分析与最佳实践
When accessing the shared folder, you will be prompted "because file sharing is not secure smb1 Protocol". Please use Smb2 protocol
Win11用户名和密码备份方法
Behavior level description and RTL level description
论文赏析[EMNLP18]针对自顶向下和中序移进归约成分句法分析的Dynamic Oracles
Feixin died in 2022: a good hand of China Mobile was broken, and 500million users became "zombies"
Force buckle 919. Complete binary tree inserter
中地数码:融合创新国产GIS 乘风而上助推实景三维中国建设
Implicit intent
Understanding Network Model OSI model