I'm having a puzzling NullReferenceException occuring intermittently, but consistently, in my application. The stack trace is:
at Be.Windows.Forms.HexBox.OnPaint(PaintEventArgs e)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Looking at HexBox source code, I don't see how this is possible, since there are numerous checks both in UpdateVisibilityBytes() and in prior functions for a null object. I'm wondering if this is some sort of race condition in my application, since I also overload OnPaint for a custom control. Any thoughts? Let me know what else I can provide to help diagnose.
UPDATE: It appears the problem goes away when I don't dispose of the byteProvider in this way:
if (hexViewer.ByteProvider != null)
Be.Windows.Forms.FileByteProvider prov = (Be.Windows.Forms.FileByteProvider)hexViewer.ByteProvider;
You should set the ByteProvider property to null before you call Dispose method…
var provider = (FileByteProvider)hexBox.ByteProvider;
hexBox.ByteProvider = null;
Unfortunately, I'm still receiving the NullReferenceException.
The callstack shows that the exception occurs in Length property of FileByteProvider
public long Length
The exception tells you that _fileStream is null. This member is set to null only after you call Dispose. So my Dispose code from above should solve this. So you did something wrong, maybe you are doing things with threads. A workaround would be checking for null in Length property…
return _fileStream != null ? _fileStream.Length : 0;
But this is not a good solution because you still are doing something wrong.
Log in to post a comment.