我的辅助函数返回一个空字符串

发布于 2025-01-15 12:35:25 字数 1383 浏览 5 评论 0原文

我正在为游戏编写一些代码,并尝试编写一个辅助函数来返回对象内的字符串:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还如梦归 2025-01-22 12:35:25

il2cppi_to_string() 返回一个临时 std::string,它将在调用 il2cppi_to_string() 的表达式末尾被销毁。您将获得一个指向该临时数据的 const char* 指针 std::string,这正是 ReSharper 向您发出的警告。由于临时 std::string返回之前被销毁,这意味着getGhostName()返回一个< em>指向无效内存的悬空指针。

要解决此问题,请将 getGhostName() 更改为返回 std::string 而不是 const char*

std::string getGhostName(GhostAI* ghostAI)
{
    if (ghostAI) {
        GhostInfo* ghostInfo = getGhostInfo(ghostAI);
        const auto ghostName = ghostInfo->fields.u0A6Du0A67u0A74u0A71u0A71u0A66u0A65u0A68u0A74u0A6Au0A6F.u0A65u0A66u0A6Eu0A67u0A69u0A74u0A69u0A65u0A74u0A6Fu0A67;
        return il2cppi_to_string(ghostName);
    }
    return "UNKNOWN";
}

il2cppi_to_string() returns a temporary std::string, which will be destroyed at the end of the expression that calls il2cppi_to_string(). You are obtaining a const char* pointer to the data of that temporary std::string, which is what ReSharper is warning you about. Since the temporary std::string is destroyed before the return, that means getGhostName() is returning a dangling pointer to invalid memory.

To fix this, change getGhostName() to return a std::string instead of a const char*:

std::string getGhostName(GhostAI* ghostAI)
{
    if (ghostAI) {
        GhostInfo* ghostInfo = getGhostInfo(ghostAI);
        const auto ghostName = ghostInfo->fields.u0A6Du0A67u0A74u0A71u0A71u0A66u0A65u0A68u0A74u0A6Au0A6F.u0A65u0A66u0A6Eu0A67u0A69u0A74u0A69u0A65u0A74u0A6Fu0A67;
        return il2cppi_to_string(ghostName);
    }
    return "UNKNOWN";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文