SGen fails when collecting a class with LayoutKind.Explicit.
public long @signed;
public string Text;
public static void Main(string args)
var array = new ExplicitLayout;
for (int i = 0; i < array.Length; i++)
array[i] = new ExplicitLayout();
array[i].@signed = i;
Mono/Sgen will crash with an exception on the invoke of GC.Collect. Most likely it is trying to collect the Text string member which isn't a valid reference.
This is by design, the GC expects that fields that hold references contain valid values.
Ok, so I guess the major difference with .NET is that it throws a TypeLoadException at start up rather than throwing in the GC when it sees the bad reference.
The full MS exception message at program start is:
Could not load type 'TestExplictLayout.ExplicitLayout' from assembly 'ConsoleApplication2, Version=188.8.131.52, Culture=neutral, PublicKeyToken=null' because it contains an object field at offset 0 that is incorrectly aligned or overlapped by a non-object field.
We should probably do the same.
I guess at least the app gets a chance to catch the System.Exception and report some meaningful info to the user then - always useful!
Fixed in mono master 8d9f05ea9a943ff1a36bb0ef3dd0521853d3a489.
The fix has to be reverted as apparently too much real-life code depends on this.
You can enable the verifier to check for this.