wchar的内容被删除
Content of wchar is deleted
我有这个代码:
JSONObject object;
if (value->IsObject())
{
object = value->AsObject();
const wchar_t *tmp = from_string(entity_id);
std::wcout << tmp << std::endl;
std::wcout.flush();
if (object.find(tmp) != object.end())
{
std::wcout << tmp << std::endl;
std::wcout.flush();
initFromJSON(object[tmp]->AsObject());
}
else
{
return false;
}
}
这里的问题是,在内部 if 语句之后,tmp 的内容是空的。在此之前不是。
当我尝试调试时,一切正常,内容没有被删除。但是当我运行程序时,内容被删除了。知道为什么吗?
方法from_string(entity_id)
只能像这样从字符串转换为wchar:
std::wstring result;
for (int i = 0; i<content.length(); i++)
result += wchar_t(content[i]);
return result.c_str();
方法JSONObject::find(...)
和JSONObject::end()
是这样的:
iterator find(const key_type& __k) {return __tree_.find(__k);}
iterator end() _NOEXCEPT {return __tree_.end();}
我认为这不是 find(...) 或 end() 中的问题。我猜这个问题出在其他地方,但我找不到它。因为if语句后wchar的内容为空我不能说
initFromJSON(object[tmp]->AsObject());
因为 object[tmp]
不存在。
任何建议我做错了什么?
问题是您的 from_string
函数 return 是指向本地实体的指针,即 std::wstring::c_str()
的 return 值。因此,您引入了未定义的行为。
我看不出 tmp
变量是指针的任何理由。您应该可以改为执行以下操作:
std::wstring from_entity(...)
{
std::wstring result;
for (int i = 0; i<content.length(); i++)
result += wchar_t(content[i]);
return result;
}
然后是这个:
JSONObject object;
if (value->IsObject())
{
object = value->AsObject();
std::wstring tmp = from_string(entity_id);
std::wcout << tmp << std::endl;
std::wcout.flush();
if (object.find(tmp.c_str()) != object.end())
{
std::wcout << tmp << std::endl;
std::wcout.flush();
initFromJSON(object[tmp.c_str()]->AsObject());
}
else
{
return false;
}
}
请注意,std::wstring
是 returned,而不是指针。然后在函数本身中,如果需要传递 const wchar_t*
.
,则使用 c_str()
我有这个代码:
JSONObject object;
if (value->IsObject())
{
object = value->AsObject();
const wchar_t *tmp = from_string(entity_id);
std::wcout << tmp << std::endl;
std::wcout.flush();
if (object.find(tmp) != object.end())
{
std::wcout << tmp << std::endl;
std::wcout.flush();
initFromJSON(object[tmp]->AsObject());
}
else
{
return false;
}
}
这里的问题是,在内部 if 语句之后,tmp 的内容是空的。在此之前不是。 当我尝试调试时,一切正常,内容没有被删除。但是当我运行程序时,内容被删除了。知道为什么吗?
方法from_string(entity_id)
只能像这样从字符串转换为wchar:
std::wstring result;
for (int i = 0; i<content.length(); i++)
result += wchar_t(content[i]);
return result.c_str();
方法JSONObject::find(...)
和JSONObject::end()
是这样的:
iterator find(const key_type& __k) {return __tree_.find(__k);}
iterator end() _NOEXCEPT {return __tree_.end();}
我认为这不是 find(...) 或 end() 中的问题。我猜这个问题出在其他地方,但我找不到它。因为if语句后wchar的内容为空我不能说
initFromJSON(object[tmp]->AsObject());
因为 object[tmp]
不存在。
任何建议我做错了什么?
问题是您的 from_string
函数 return 是指向本地实体的指针,即 std::wstring::c_str()
的 return 值。因此,您引入了未定义的行为。
我看不出 tmp
变量是指针的任何理由。您应该可以改为执行以下操作:
std::wstring from_entity(...)
{
std::wstring result;
for (int i = 0; i<content.length(); i++)
result += wchar_t(content[i]);
return result;
}
然后是这个:
JSONObject object;
if (value->IsObject())
{
object = value->AsObject();
std::wstring tmp = from_string(entity_id);
std::wcout << tmp << std::endl;
std::wcout.flush();
if (object.find(tmp.c_str()) != object.end())
{
std::wcout << tmp << std::endl;
std::wcout.flush();
initFromJSON(object[tmp.c_str()]->AsObject());
}
else
{
return false;
}
}
请注意,std::wstring
是 returned,而不是指针。然后在函数本身中,如果需要传递 const wchar_t*
.
c_str()