CppSharp icon indicating copy to clipboard operation
CppSharp copied to clipboard

2-dimensional arrays inside structs not generated properly

Open zghonda opened this issue 5 years ago • 6 comments

Brief Description

Hello,

I noticed that the binding code for my 2-dimensional array used to represent a matrix is not correctly generated.

So my question is : Do you support multidimensional arrays ?

Here's a small code to illustrate the problem

Thank you in advance

OS: Windows, binaries compiled for amd64

Used headers
__declspec(dllexport) struct matrix3x3{
    float matrix[3][3];
};

Used settings

Exactly the same as the official tutorial

Target: MSVC

Generated code

(No accessors for my matrix)

namespace MatrixIssue
{
    public unsafe partial class Matrix3x3 : IDisposable
    {
        [StructLayout(LayoutKind.Explicit, Size = 36)]
        public partial struct __Internal
        {
            [FieldOffset(0)]
            internal fixed float matrix[9];

            [SuppressUnmanagedCodeSecurity]
            [DllImport("MatrixIssue", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
                EntryPoint="??0matrix3x3@@QEAA@AEBU0@@Z")]
            internal static extern global::System.IntPtr cctor(global::System.IntPtr __instance, global::System.IntPtr _0);
        }

        public global::System.IntPtr __Instance { get; protected set; }

        protected int __PointerAdjustment;
        internal static readonly global::System.Collections.Concurrent.ConcurrentDictionary<IntPtr, global::MatrixIssue.Matrix3x3> NativeToManagedMap = new global::System.Collections.Concurrent.ConcurrentDictionary<IntPtr, global::MatrixIssue.Matrix3x3>();
        protected internal void*[] __OriginalVTables;

        protected bool __ownsNativeInstance;

        internal static global::MatrixIssue.Matrix3x3 __CreateInstance(global::System.IntPtr native, bool skipVTables = false)
        {
            return new global::MatrixIssue.Matrix3x3(native.ToPointer(), skipVTables);
        }

        internal static global::MatrixIssue.Matrix3x3 __CreateInstance(global::MatrixIssue.Matrix3x3.__Internal native, bool skipVTables = false)
        {
            return new global::MatrixIssue.Matrix3x3(native, skipVTables);
        }

        private static void* __CopyValue(global::MatrixIssue.Matrix3x3.__Internal native)
        {
            var ret = Marshal.AllocHGlobal(sizeof(global::MatrixIssue.Matrix3x3.__Internal));
            *(global::MatrixIssue.Matrix3x3.__Internal*) ret = native;
            return ret.ToPointer();
        }

        private Matrix3x3(global::MatrixIssue.Matrix3x3.__Internal native, bool skipVTables = false)
            : this(__CopyValue(native), skipVTables)
        {
            __ownsNativeInstance = true;
            NativeToManagedMap[__Instance] = this;
        }

        protected Matrix3x3(void* native, bool skipVTables = false)
        {
            if (native == null)
                return;
            __Instance = new global::System.IntPtr(native);
        }

        public Matrix3x3()
        {
            __Instance = Marshal.AllocHGlobal(sizeof(global::MatrixIssue.Matrix3x3.__Internal));
            __ownsNativeInstance = true;
            NativeToManagedMap[__Instance] = this;
        }

        public Matrix3x3(global::MatrixIssue.Matrix3x3 _0)
        {
            __Instance = Marshal.AllocHGlobal(sizeof(global::MatrixIssue.Matrix3x3.__Internal));
            __ownsNativeInstance = true;
            NativeToManagedMap[__Instance] = this;
            *((global::MatrixIssue.Matrix3x3.__Internal*) __Instance) = *((global::MatrixIssue.Matrix3x3.__Internal*) _0.__Instance);
        }

        public void Dispose()
        {
            Dispose(disposing: true);
        }

        public virtual void Dispose(bool disposing)
        {
            if (__Instance == IntPtr.Zero)
                return;
            global::MatrixIssue.Matrix3x3 __dummy;
            NativeToManagedMap.TryRemove(__Instance, out __dummy);
            if (__ownsNativeInstance)
                Marshal.FreeHGlobal(__Instance);
            __Instance = IntPtr.Zero;
        }
    }
}

zghonda avatar Aug 07 '19 14:08 zghonda

Guess we're skipping generation of these.

Some thought needs to be done for the best way to interop these.

How would you like the accessor to look like?

C# has multi-dimensional arrays, but I guess we'd have to do a deep copy for each access.

So I think it might be best if we generate a multi-dimensional indexer. Something along the lines of:

    public int this[int index1, int index2] 
    { 
        get 
        { 
            return data[index1, index2];               
        } 
          
        set 
        {               
            data[index1, index2] = value;               
        } 
    } 
} 

With the necessary marshaling code of course.

Would you be able to help implement this @zghonda?

Seems like it would be relatively easy to add.

cc @ddobrev

tritao avatar Aug 07 '19 16:08 tritao

@tritao your suggestion definitely is the solution. @zghonda our free time is limited, however, so I cannot promise you any deadline. Alternatively you could try yourself or contact our support.

ddobrev avatar Aug 07 '19 16:08 ddobrev

Hi,

My apologies for the late answer.

We found a workaround for the problem, Unfortunately my free time is also limited but I'll let you know when I'm ready to help and return the favor.

Thank you anyway,

zghonda avatar Aug 15 '19 11:08 zghonda

@tritao Hi, just a friendly inquiry. During the code generation, we also noticed this bug. Is there already a plan from your side on whether this bug will be fixed and if so, when? For us, this is an important functionality.

Thanks in advance

XzuluX avatar Dec 20 '23 08:12 XzuluX

At the moment I don't have any plan to personally fix this, there are other more critical bugs to fix with the limited time.

@XzuluX Any chance you can take a look?

tritao avatar Dec 20 '23 12:12 tritao

@tritao Perhaps I will have some time in a few weeks to take a look at it ...

XzuluX avatar Dec 20 '23 14:12 XzuluX