[Mono-list] LayoutKind.Explicit marshaling problem

J. Perkins jason@379.com
10 Jan 2003 08:34:27 -0500


I am having trouble wrapping the X11 XNextEvent() function with Mono.
This problem appears to have been first reported by Everaldo Canuto back
in September (http://makeashorterlink.com/?V60442903). I have not tried
compiling X# but I assume it would have the same problem.

The assert fires when trying to build an equivalent to the XEvent class,
which is a union of structures. Passing an XEvent to a DllImport causes
an assertion in marshal.c. This is using the 2003-Jan-09 snapshot on a
RH8 box. I could not find any mention of this problem in bugzilla, even
though it was initially reported a few months ago. Is this a known
problem? If not I will file a new bug. The assertion message and the
sample code follows.

Note that removing the second member from XEvent causes the problem to
disappear, it is the second [FieldOffset] that triggers the assert.

Jason
379

** ERROR **: file marshal.c: line 803 (emit_struct_conv): assertion
failed: (msize > 0 && usize > 0)

using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
public struct XAnyEvent
{
	public int type;
}

[StructLayout(LayoutKind.Sequential)]
public struct XEventPadding
{
	int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
}

[StructLayout(LayoutKind.Explicit)]
public struct XEvent
{
	[FieldOffset(0)] public  XAnyEvent xany;
	[FieldOffset(0)] private XEventPadding xpad;
}

public class ExplicitBug
{
	[DllImport("X11")] 
	static extern IntPtr XOpenDisplay(string name);

	[DllImport("X11")] 
	static extern int XNextEvent(IntPtr dpy, out XEvent xevent);
	
	static void Main()
	{
		IntPtr dpy = XOpenDisplay(null);
		
		XEvent evt = new XEvent();
		XNextEvent(dpy, out evt);
	}
}