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 · 0 comments
Open

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

svermeulen opened this issue May 10, 2024 · 0 comments

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,
        };
    }
}
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

1 participant