Search code examples
f#monovisual-studio-macf#-interactive

F# interactive Marshal.SizeOf with Mono throws “assertion” error


I am trying to read a GIF file header into a structure with F#, using Mono 5.8 on Mac OSX. The following code sample works fine in Visual Studio 2017 on Windows 10; however, when I try to run it in Visual Studio for Mac, I get the following error in F# interactive:

* Assertion at class-accessors.c:138, condition `mono_class_has_static_metadata (klass)' not met

The code I am using is below. The structure is probably incorrect since I just threw it together quickly for the question, but

open System
open System.IO
open System.Runtime.InteropServices

[<Struct; StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)>]
type GifHeader = {
    [<MarshalAs(UnmanagedType.ByValTStr, SizeConst = 3)>]
    signature: string
    [<MarshalAs(UnmanagedType.ByValTStr, SizeConst = 3)>]
    version: string
    logicalWidth: int16
    logicalHeight: int16
    }

When I invoke Marshal.SizeOf(typeof<GifHeader>) in Visual Studio for Mac's FSI, I get the error mentioned above.

I noticed that when I am reading into a structure like:

[<Struct; StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)>]
type SomeOtherHeader = {
    field1: uint16
    field2: int32
    field3: int16
    field4: float
    }

i.e. with no MarshalAs UnmanagedType specification, the error is not thrown.

Does anyone know what this error means? I haven't seen any other instances of this particular error on Google or other SE posts, and it is especially puzzling since it doesn't occur on Windows/.NET Framework 4.7. Not having the interactive window to test code is a massive hindrance for learning, and quite frustrating since I don't think it's possible to use an alternative (non-Mono) FSI.


Solution

  • This was an issue intrinsic to the current Mono release. Per the Mono team, this has been fixed as of Mono 5.14.