Skip to content

格式化代码, 修复 Clang 不能编译, 修复 il2cpp_thread_attach 可能导致的崩溃, 修复 il2cpp_type_get_name 导致的内存泄漏 #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

TrialCarrot
Copy link

根据 Unity 2022.3.59f1 的源代码, il2cpp_type_get_name 的定义如下:

char* il2cpp_type_get_name(const Il2CppType *type)
{
    std::string name = Type::GetName(type, IL2CPP_TYPE_NAME_FORMAT_IL);
    char* buffer = static_cast<char*>(il2cpp_alloc(name.length() + 1));
    memcpy(buffer, name.c_str(), name.length() + 1);

    return buffer;
}

其中 il2cpp_alloc 实际上(默认)调用 malloc , 也就是在堆上分配新内存, 而 C++ std::string 对于从 (const) char * 构造的行为是复制指针所指向的字符串的内容, 但不会负责释放该指针, 所以我们需要调用 il2cpp_free 显式释放返回值, 避免内存泄漏.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant