Registry Mirror
Skip to content

Passing this should be passed as a pointer #239

Closed

Description

@NickAcPT

Repro header:

struct TestInterface_;
struct MyStruct_;

#ifdef __cplusplus
typedef MyStruct_ MyStruct;
#endif

struct TestInterface_ {
    int (__stdcall *TestMethod)(MyStruct *vm);
};

struct MyStruct_ {
    const struct TestInterface_ *functions;
#ifdef __cplusplus
    int TestMethod() {
        return functions->TestMethod(this);
    }
#endif
};

Generated (Invalid) C# code:

public unsafe partial struct TestInterface_
{
    [NativeTypeName("int (*)(MyStruct *) __attribute__((stdcall))")]
    public delegate* unmanaged[Cdecl]<MyStruct_*, int> TestMethod;
}

public unsafe partial struct MyStruct_
{
    [NativeTypeName("const struct TestInterface_ *")]
    public TestInterface_* functions;

    public int TestMethod()
    {
        return functions->TestMethod(this);
    }
}

Activity

  1. TechInterMezzo commented on May 26, 2026

    @TechInterMezzo

    This bug still exists. @tannergooding Any news on this?

  2. tannergooding commented on May 26, 2026

    @tannergooding
    Member

    Its not something I've explicitly looked at yet, no. Anyone is welcome to contribute a fix, however.

    C++ is difficult to get correct for many reasons and there are a number of edge cases that will be difficult to handle. This is one that requires an explicit fixed to be emitted and should handle most of the scenarios. That being said, the more general problem is that it requires recognizing if any statement in the method body takes the address of this and then fixing for the entire scope, as otherwise there may be lifetime issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions