2

I have a strange error, which has started suddenly and which is driving me crazy. In my C# application, I am doing some heavy mathematical calculations and for that purpose, I am using CenterSpace's NMath library. Recently, when I started my application from inside Visual Studio 2015 in order to debug it, the application started to crash because of NullReferenceExceptions in NMath.dll, whenever I call an NMath library method (and another library which we have written, as well). In order to isolate the problem, I just run the following very basic matrix multiplication code:

    static void Main(string[] args)
    {
        var mA = new DoubleMatrix(100, 200, 1.5);
        var mB = new DoubleMatrix(200, 50, 1.2);
        var mC = NMathFunctions.Product(mA, mB);
        Console.WriteLine(mC[5, 6]);

        Console.WriteLine("Computation finished");
        Console.ReadLine();
    }

The code crashes in the third line, when I call the "NMathFunctions.Product" method because of a NullReferenceException, with the following exception detail: enter image description here

It seems that it crashes just in the constructor of the "NMathKernel" object, since an object reference is not properly set.

The most strange thing is, when I run the exactly same program directly from its project folder by using Windows Explorer, not within Visual Studio, the program runs perfectly: The fourth line prints "360.000000001" as expected and the "Console.ReadLine()" is reached!

I am completely clueless about the reason of this NullReferenceException error. It is not a third party dll related problem it seems, since the program only crashes when started from within the Visual Studio. This error completely blocks me from debugging my program with Visual Studio, which is a painful thing, leaves me the only option of debugging it with "Console.WriteLine" calls, from outside of Visual Studio. What can be the reason of this problem? Is something messed up with my Build settings maybe? (Which I have checked and found nothing out of order by the way) Any clues or hints are so much welcome.

Ufuk Can Bicici
  • 3,589
  • 4
  • 28
  • 57
  • 1
    Posting the stacktrace instead of a screenshot would really be more helpful – torvin Sep 24 '15 at 00:22
  • Exceptions that occur when debugging but not otherwise often are caused by one of two reasons: the code is specifically checking for an attached debugger and crashing on purpose (used as a rudimentary copy-protection scheme); or there is some unmanaged code somewhere doing the wrong thing, in a way that is caught in an un-optimized build (attaching the debugger disables optimizations) but not otherwise (i.e. there's a bug, it still happens even without the debugger attached, but the code gets lucky and doesn't crash). In any case, you need to have the NMath authors answer your question. – Peter Duniho Sep 24 '15 at 00:33
  • @PeterDuniho I think VS is just catching the exception before it got handled by the library code. See my answer below. – torvin Sep 24 '15 at 00:36

1 Answers1

4
  1. In VisualStudio go to Tools -> Options -> Debugging and make sure you have Enable Just My Code option checked.
  2. Go to Debug -> Exceptions and unckeck all Thrown chechboxes. Only leave User-unhandled checkboxes checked. Or just use the Reset all button.
torvin
  • 6,515
  • 1
  • 37
  • 52
  • Thank you so much. All Exceptions are somehow marked to be thrown in the Visual Studio settings, so even exceptions of third party libraries which are otherwise handled perfectly by those libraries are caught and thrown by VS. Now the program seems to work. You saved me from hours of fruitless debugging effort my friend. – Ufuk Can Bicici Sep 24 '15 at 00:47
  • Ha, my psychic powers come in handy again! – torvin Sep 24 '15 at 01:10
  • Thank you so much. I have no idea how Just My Code got unchecked but I've been pulling my hair out not being able to use the debugger, and this fixed it. Still relevant five years later! – Collin Brittain Jul 03 '20 at 00:47