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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With