Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no stack trace on NullReferenceException

Tags:

c#

I have an application that terminates with a NullReferenceException when I run it from the console in release mode. It runs fine in debug mode or in the debugger. The program terminates with the following output: Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.

No stack trace is shown, A visual studio just in time debugger dialog also pops up, however it also has no stack trace.

The pdb files are present.

How can I get visual studio to give me a stack trace?

I have tried setting AppDomain.CurrentDomain.UnhandledException, which does not catch the exception, as well as try/catch everywhere.

This is a console app, which uses a csharp dll, which in turn uses a c++ dll.

I know what a NullReferenceException is, and how to fix it - I just can't find it

like image 904
user5497015 Avatar asked Oct 18 '22 23:10

user5497015


1 Answers

which in turn uses a c++ dll

That's the problem. Native C++ code can also fail on "null references", a very common bug in a language where pointers are first-class citizens. It gets reported the exact same way. A bit unfortunate, ought to be an AccessViolationException but the CLR is not selective enough to limit translating it to NRE for only managed code.

And yes, no usable stack trace. Such is the price of using C++, it primary reason for being is to generate code as fast as possible, at the cost of diagnostics. A stack trace requires the call stack to be reliably walkable, a rock-hard requirement for managed code since both CAS and the garbage collector depend on being able to peek at stack frames. Not for free, perhaps a few percent of perf, too much for a C++ compiler.

You need to use a debugger. Enable the unmanaged debugger with Project > Properties > Debug tab > tick the "Enable native code debugging" checkbox. If you are catching this exception then you need to force the debugger to stop when it is thrown, Debug > Exceptions > tick the "Win32 Exceptions" checkbox. If you can't repro this crash on your own machine then you need a minidump, generate one with, say, DebugDiag.

like image 162
Hans Passant Avatar answered Oct 21 '22 15:10

Hans Passant