NullReferenceException after OnPaint

Help
sippy
2011-07-01
2013-04-30
  • sippy

    sippy - 2011-07-01

    Hi again,

    I'm having a puzzling NullReferenceException occuring intermittently, but consistently, in my application.  The stack trace is:

       at Be.Windows.Forms.FileByteProvider.get_Length()
       at Be.Windows.Forms.HexBox.UpdateVisibilityBytes()
       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.

    Thanks,

     
  • sippy

    sippy - 2011-07-01

    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;
           prov.Dispose();
            hexViewer.Refresh();
    }

    Why?

     
  • Bernhard Elbl

    Bernhard Elbl - 2011-07-04

    You should set the ByteProvider property to null before you call Dispose method…
    var provider = (FileByteProvider)hexBox.ByteProvider;
    hexBox.ByteProvider = null;
    provider.Dispose();

     
  • sippy

    sippy - 2011-07-11

    Unfortunately, I'm still receiving the NullReferenceException.

     
  • Bernhard Elbl

    Bernhard Elbl - 2011-07-12

    The callstack shows that the exception occurs in Length property of FileByteProvider
    public long Length
    {
    get
    {
    return _fileStream.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.