views:

61

answers:

2

In my solution of VS2010, a C# app project references a F# library project.

When a NullReferenceException is thrown from F# lib, the debugger cannot find the point exception thrown. It just says 'No Source Available'.

Should I change some options or this is a limitation of VS2010?

I added some example code:

F# project 'Library1'

module Module1

type AA() =
    let _a = "xx"
    member x.a = _a

let aa:AA option = None

let b() =
    aa.Value.a  // null reference occurs here

C# project 'ConsoleApp1'

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.Out.Write(Module1.b());
            Console.In.Read();
        }
    }
}
+2  A: 

This should, in principle, work without seting any additional options. A few things to check:

  • Can the runtime locate the PDB file for your F# library?

    It should do that if the PDB file is in the same directory as the DLL. You can also look at the "Output" window and select "Debug" from the combo to see information printed when the application started - there you can see whether the symbols were loaded or not. If you have PDB symbols in some special directory, you can specify the directory in Tools -> Options -> Debugging -> Symbols.

  • What happens when you try to set a breakpoint in the F# source?

    Does it say something like "symbols not loaded" (and show the breakpoint in gray color)? Does it shown any additional information that could be used to find the cause of the problem?

Tomas Petricek
Thnx Tomas. I checked that the pdb is in the same directory and the output window says symbols of the F# library are loaded. Breakpoint also works well. But still 'no source available'I added some example code in my question. Thnx.
tk
Probably not the cause of the problem (examples were added after this answer), but a +1 from me, because useful debugging tips are useful.
cfern
A: 

The error isn't caused by any C#/F# interop issues. When you call b(), it tries to access the Value of a None option value, which throws a null reference exception at runtime.

In the F# lib, try replacing let aa:AA option = None with let aa:AA option = Some(AA()). The C# code should then print 'xx'.

When exposing an F# option type to the outside world, you could allow a null return value from an empty option instead of an exception. For example:

module Module1

//attribute needed in order to be able to return null from the match statement
[<AllowNullLiteralAttribute>] 
type AA() =
    let _a = "xx"
    member x.a = _a

let aa:AA option = None

let b() = match aa with
          | None -> null
          | Some value -> value 

And check for null in any code that uses this F# library.

Note the AllowNullLiteral attribute. Try commenting out this attribute and you'll see that F# won't recognize the value null in the first match branch.

cfern
Thnx, cfern. Actually, my question is about the debugging. My code use many <T' option> values, and i guess one of them is making null reference exception, but i can't find it easily. I may need to do type matching whenever i use the <T' option> type-values.
tk