Skip to content
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

无法在包含 IntPtr 的结构体上使用 GCOptimize #1141

Open
svermeulen opened this issue May 10, 2024 · 1 comment
Open

无法在包含 IntPtr 的结构体上使用 GCOptimize #1141

svermeulen opened this issue May 10, 2024 · 1 comment

Comments

@svermeulen
Copy link

我正在使用 GCOptimize 特性在 C# 和 Lua 之间传递结构体,以避免内存分配。但是在我的一些数据中,我存储了一个 IntPtr 值。这似乎导致周围的结构体被装箱为用户数据,而不是按值传递。例如:

[GCOptimize]
public struct Bar
{
    public IntPtr Daz;
}

public static class LuaApiTests
{
    // 当从 Lua 调用时,这个函数工作时不会有任何内存分配
    public static IntPtr Foo()
    {
        return (IntPtr)123;
    }

    // 当从 Lua 调用时,这个函数会导致内存垃圾分配
    public static Bar Bar()
    {
        return new Bar()
        {
            Daz = (IntPtr)123,
        };
    }
}
@chexiongsheng
Copy link
Collaborator

这里需要在生成代码阶段计算struct的大小:https://github.com/Tencent/xLua/blob/master/Assets/XLua/Src/Editor/Generator.cs#L1540
但IntPtr的大小和运行环境有关。32位机器和64位机器不一样。

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

No branches or pull requests

2 participants