在下面的方法中运行会弹出0x00007FF6F1215BAD 处有未经处理的异常(在 YANG Auto Notice.exe 中): 堆栈 Cookie 检测代码检测到基于堆栈的缓冲区溢出。在debug运行没有报错,转release报错,我要如何解决
void CYANGRemindDlg::netlist()
{
// TODO: 在此添加控件通知处理程序代码
// TODO: 在此添加控件通知处理程序代码
CInternetSession session(_T("HttpClient"));
TCHAR* url = _T("https://v1.jinrishici.com/shuqing/shanggan");
CHttpFile* pfile = (CHttpFile*)session.OpenURL(url);
DWORD dwStatusCode;
pfile->QueryInfoStatusCode(dwStatusCode);
if (dwStatusCode == HTTP_STATUS_OK)
{
CString content4;
CString data;
while (pfile->ReadString(data))
{
content4 += data;
}
char* pch;
pch = new char[content4.GetLength() * sizeof(TCHAR) + 1];
memcpy(pch, content4, content4.GetLength() * sizeof(TCHAR));
content4 = UTF8ToUnicode(pch);//转换编码,不然就乱码了
CString str3 = content4;
Json::Reader reader23;
Json::Value root23;
CString temp23;
CString temp223;
CString temp323;
USES_CONVERSION;
char* cJson1 = T2A(str3.GetBuffer(0));
str3.ReleaseBuffer();
if (reader23.parse(cJson1, root23)) {
temp23 = root23["content"].asCString();
temp223 = root23["origin"].asCString();
temp323 = root23["author"].asCString();
}
temp223 = temp223 + " 作者:" + temp323;
num2 = temp23;
AfxGetApp()->WriteProfileString(_T("Settings"), _T("LocalRecPath2"), num2);//_localRecPath就是要保存的信息,Settings为注册表子键,LocalRecPath即子键的键值名
num = temp223;
AfxGetApp()->WriteProfileString(_T("Settings"), _T("LocalRecPath"), num);//_localRecPath就是要保存的信息,Settings为注册表子键,LocalRecPath即子键的键值名
}
pfile->Close();
delete pfile;
session.Close();
//char* str = "content春如旧。人空瘦。泪痕红浥鲛绡透。";
// "origin" : "钗头凤·红酥手",
// "author" : "陆游",
// "category" : "古诗文-抒情-伤感";
TZ();
}
检查发现引发错误的代码是
char* cJson1 = T2A(str3.GetBuffer(0));
str3.ReleaseBuffer();
if (reader23.parse(cJson1, root23)) {
temp23 = root23["content"].asCString();
temp223 = root23["origin"].asCString();
temp323 = root23["author"].asCString();
}
以下答案引用自GPT-3大模型,请合理使用:
修改意见,建议
分析原因可以由0x00007FF6F1215BAD这个异常地址找到真正抛出异常的代码段,可以通过调试发现,程序在release模式下cJson1内存申请失败了,导致抛出异常。
可以根据这个问题的具体情况,将T2A(str3.GetBuffer(0))改为str3.GetBuffer(0),相当于不申请内存。
修改之后的代码如下:
char* cJson1 = str3.GetBuffer(0);
str3.ReleaseBuffer();
if (reader23.parse(cJson1, root23)) {
temp23 = root23["content"].asCString();
temp223 = root23["origin"].asCString();
temp323 = root23["author"].asCString();
}
如果我的回答解决了您的问题,请采纳我的回答
不知道你这个问题是否已经解决, 如果还没有解决的话: