我的辅助函数返回一个空字符串
我正在为游戏编写一些代码,并尝试编写一个辅助函数来返回对象内的字符串:
const char* getGhostName(GhostAI* ghostAI)
{
if (ghostAI) {
GhostInfo* ghostInfo = getGhostInfo(ghostAI);
const auto ghostName = ghostInfo->fields.u0A6Du0A67u0A74u0A71u0A71u0A66u0A65u0A68u0A74u0A6Au0A6F.u0A65u0A66u0A6Eu0A67u0A69u0A74u0A69u0A65u0A74u0A6Fu0A67;
const char* name = il2cppi_to_string(ghostName).c_str();
return name;
}
return "UNKNOWN";
}
这是 il2cppi_to_string
函数:
std::string il2cppi_to_string(Il2CppString* str) {
std::u16string u16(reinterpret_cast<const char16_t*>(str->chars));
return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(u16);
}
std::string il2cppi_to_string(app::String* str) {
return il2cppi_to_string(reinterpret_cast<Il2CppString*>(str));
}
当我调用 getGhostName< /code>,我最终得到一个空字符串。现在我确实收到了来自 ReSharper 的警告,其中写着:
支持指针的对象将在完整表达式末尾被销毁。
当调用 il2cppi_to_string
时,此问题出现在 getGhostName
内的以下行中:
const char* name = il2cppi_to_string(ghostName).c_str();
我不完全确定这意味着什么或如何修改代码来修复它。我绝对讨厌在 C++ 中使用字符串。
I'm writing some code for a game, and I'm attempting to write a helper function to return a string inside an object:
const char* getGhostName(GhostAI* ghostAI)
{
if (ghostAI) {
GhostInfo* ghostInfo = getGhostInfo(ghostAI);
const auto ghostName = ghostInfo->fields.u0A6Du0A67u0A74u0A71u0A71u0A66u0A65u0A68u0A74u0A6Au0A6F.u0A65u0A66u0A6Eu0A67u0A69u0A74u0A69u0A65u0A74u0A6Fu0A67;
const char* name = il2cppi_to_string(ghostName).c_str();
return name;
}
return "UNKNOWN";
}
And here is the il2cppi_to_string
functions:
std::string il2cppi_to_string(Il2CppString* str) {
std::u16string u16(reinterpret_cast<const char16_t*>(str->chars));
return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(u16);
}
std::string il2cppi_to_string(app::String* str) {
return il2cppi_to_string(reinterpret_cast<Il2CppString*>(str));
}
When I call getGhostName
, I end up with an empty string. Now I did get a warning from ReSharper which says:
Object backing the pointer will be destroyed at the end of the full-expression.
This is appearing on the following line inside getGhostName
when calling il2cppi_to_string
:
const char* name = il2cppi_to_string(ghostName).c_str();
I'm not entirely sure what this means or how I can modify the code to fix it. I absolutely hate working with strings in C++.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
il2cppi_to_string()
返回一个临时std::string
,它将在调用il2cppi_to_string() 的表达式末尾被销毁
。您将获得一个指向该临时数据的const char*
指针std::string
,这正是 ReSharper 向您发出的警告。由于临时std::string
在返回
之前被销毁,这意味着getGhostName()
返回一个< em>指向无效内存的悬空指针。要解决此问题,请将
getGhostName()
更改为返回std::string
而不是const char*
:il2cppi_to_string()
returns a temporarystd::string
, which will be destroyed at the end of the expression that callsil2cppi_to_string()
. You are obtaining aconst char*
pointer to the data of that temporarystd::string
, which is what ReSharper is warning you about. Since the temporarystd::string
is destroyed before thereturn
, that meansgetGhostName()
is returning a dangling pointer to invalid memory.To fix this, change
getGhostName()
to return astd::string
instead of aconst char*
: