Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullReferenceException, no stack trace... where to start?

I have a WPF audio application.

Occasionally (and even in the debugger) I'm seeing a NullReferenceException which carries no stack trace information with it.

How can one start debugging such an issue?

Some background:

I'm P/Invoking functions in WinMM.dll which involves registering a callback when calling waveOutOpen

    [DllImport("winmm.dll")]
    public static extern MmResult waveOutOpen(out IntPtr phwo, IntPtr uDeviceID, WaveFmt pwfx, WaveCallbk dwCallback, IntPtr dwInstance, int fdwOpen);

This has proved difficult to get stable, especially at the point where I call waveOutClose, and immediately call waveOutOpen again (usually to change the output format).

I suspect that the problem may be related to the calls I've been describing above (although on so little knowlege, I could be completely off-target).

Reproducing the problem is currently proving elusive, although I can supply a build to a user who is fairly consistently seeing this problem. I might try to speed up the operations that are causing the problem to the point where reproducing the problem in the debugger is more of a certainty.

With regards to the debugger, I haven't tinkered with any of the settings (including Enable unmanaged code debugging) or any of the Debug -> Exceptions... settings. To be honest, I'm fairly clueless about what's on offer here, so any hints are welcome.

How might an exception not have a stack trace? Have you seen this before? Help!

like image 820
spender Avatar asked Apr 11 '11 11:04

spender


People also ask

How do you handle NullReferenceException?

Solutions to fix the NullReferenceException To prevent the NullReferenceException exception, check whether the reference type parameters are null or not before accessing them. In the above example, if(cities == null) checks whether the cities object is null or not.

How do I fix this error system NullReferenceException object reference not set to an instance of an object?

The best way to avoid the "NullReferenceException: Object reference not set to an instance of an object” error is to check the values of all variables while coding. You can also use a simple if-else statement to check for null values, such as if (numbers!= null) to avoid this exception.

Can NullReferenceException be caught?

Find out which exception you're receiving, and either catch that specific exception, or else prevent it from occurring in the first place. You should never catch NullReferenceException.

What is NullReferenceException in C#?

NullReferenceException is thrown in C# when you try to access a property of method on an object of null reference. Hence the name Null Reference. Simply put, the object is null. The object here could be a string, a class object, or anything.


1 Answers

The most helpful thing you can do in the debugger is instruct it to break on a first-chance exception (Debug -> Exceptions):

VS Exceptions window

This will cause a break into the debugger at the exact point a NullReferenceException is thrown, which is pretty much the best you can ever hope for while debugging.

like image 124
Jon Avatar answered Oct 07 '22 03:10

Jon