BadImageFormatException when running F# application
I sent a bug report describing an issue which caused BadImageFormatException to be thrown to the fsbugs@microsoft.com email address in November of last year.

They replied the next day with a confirmation that it demonstrated a bug, and an assurance that they'd "follow it up at their end". There doesn't seem to be a publicly accessible issue tracker associated with that email address, so I've reproduced the report below. Hopefully it'll help you to refactor in order to work around the bug.

Bug report


Compiling the following code with optimization disabled using the following command line invoking the v3.1 compiler:

    fsc.exe --optimize- FSharpBadImageFormatException.fs

results in an executable which raises System.BadImageFormatException when the call to "g x" (commented below) is made:

type T() =
    member this.H<[<Measure>]'u> (x : int<'u>) = x

    member this.F<[<Measure>]'u> (x : int<'u>) =
        let g x =
            this.H x

        g x // Raises System.BadImageFormatException

[<Measure>] type M

[<EntryPoint>]
let main argv = 
    let result = T().F (LanguagePrimitives.Int32WithMeasure<M> 0)
    printfn "Result: %A" result
    0

Here's a few more details that might help you track down the cause of the problem.

The problem does not occur if any of the following changes are made:
  1. If the type annotations on T.H are removed
  2. If the v3.0 compiler is used
  3. If optimization is enabled
  4. If the function g is moved from the scope of T.F to the scope of T
By on 3/14/2014 9:25 AM ()
I just spent 8 hours trying to debug a problem like this. I think I may have encountered the same bug.

Here is my (non-reduced) case (the following throws a System.BadImageFormatException within the Seq.map callback):
    /// calculate centre point from rect    
    let centroids (rs:seq<EventRect>)  =
        let centre s (l:float<_>) = s + (l / 2.0)
        Seq.map (fun r -> (centre (left r) (oldWidth r), centre (bottom r) (height2 r.Top r.Bottom))) rs

This does not raise the exception:
    /// calculate centre point from rect
    let centre s (l:float<_>) = s + (l / 2.0)    
    let centroids (rs:seq<EventRect>)  =
        Seq.map (fun r -> (centre (left r) (oldWidth r), centre (bottom r) (height2 r.Top r.Bottom))) rs

VS2013, .NET Framework Version 4. F# 3.0 runtime (FSharp.Core, 4.3.0.0).
The bug only occurred within NCrunch while running xUnit tests.

Was there somewhere (or thread or issues) where I should report this bug?
By on 7/31/2014 4:36 AM ()
IntelliFactory Offices Copyright (c) 2024 IntelliFactory. All rights reserved.
| | | Trainings | | |
Built with