当前位置:网站首页>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 .
边栏推荐
- 中地数码:融合创新国产GIS 乘风而上助推实景三维中国建设
- A lock faster than read-write lock. Don't get to know it quickly
- ACM MM 2022 | 浙大提出:点云分割主动学习新SOTA
- Natapp intranet penetration tool Internet access personal projects
- When accessing the shared folder, you will be prompted "because file sharing is not secure smb1 Protocol". Please use Smb2 protocol
- Rust变量特点
- [anti shake and throttling]
- Conquer 3 pieces of IT equipment for all programmers →
- PHP code audit 5 - XSS vulnerability
- 报表设计丨如何让你的PowerBI看板出彩?
猜你喜欢

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

Force buckle 919. Complete binary tree inserter

Conquer 3 pieces of IT equipment for all programmers →
![论文赏析[EMNLP18]用序列标注来进行成分句法分析](/img/99/98f3e5795407c764907e957f69df10.png)
论文赏析[EMNLP18]用序列标注来进行成分句法分析

Leetcode daily practice - 203. remove linked list elements

图解 SQL,这也太形象了吧!

How to talk to CIO / CTO

Leetcode daily practice 206. Reverse the linked list

Leetcode-136-a number that appears only once

MAPGIS 3D pipeline modeling awakens the pulse of urban underground pipelines
随机推荐
Postgresql源码(65)新快照体系Globalvis工作原理分析
常见ArrayLIst面试题
Sscanf caused the address to be out of bounds
Understand the communication mode of transmission media
Leetcode daily practice - the penultimate node in the linked list
ADB shell LS /system/bin (index table)
报表设计丨如何让你的PowerBI看板出彩?
中英文说明书丨人甲胎蛋白(AFP)ELISA定量试剂盒
Understanding Network Model OSI model
Stick to one thing
说明书丨Worthington逆转录酶、重组 HIV 检测方案
redis cook book.notes.
R language dplyr package summary_ The at function calculates the count number, mean and median of multiple data columns (specified by vectors) in the dataframe data, and specifies the function list us
LeetCode每日一练 —— 21. 合并两个有序链表
R language uses t The test function performs a t-test to verify whether the population mean is a specific value (inferring the population mean from the sample set)
“地理-语言”大模型文心ERNIE-GeoL及应用
The maximum recommended number of rows for MySQL is 2000W. Is it reliable?
Worthington蘑菇多酚氧化酶的特性及测定方案
LeetCode每日一练 —— 206. 反转链表
MapGIS三维管线建模,唤醒城市地下管线脉搏