Random unexplicable run-time error : System.AccessViolationException: Attempted to read or write protected memory RRS feed

  • Question

  • I am currently developing and maintaining a server client application with UDP sockets and serial(RS232) data aquisition. I am getting a random error witch  is GUI Based, I think , and i cannot find the source of it.

    System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
       at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
       at System.Windows.Forms.Control.DefWndProc(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
       at System.Windows.Forms.RichTextBox.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    The source code is written in C# and i did took all the thread safe precautions i could think of.
    Where exactly should I start looking for the source of the exception?
    I used the
        Application.ThreadException and AppDomain.CurrentDomain.UnhandledException event handlers to catch all unpredicted exceptions.
    Wednesday, July 23, 2008 12:11 PM


  • Well, it is certainly going to be tough.  Your RichTextBox control is bombing when it processes a Windows message.  Even if you do manage to capture the unmanaged stack walk with an unmanaged debugger, you'd be looking at raw machine code inside of Windows.  Considering you are using sockets and serial ports, the chance for internal state corruption due to a threading problem are likely.  If the stack walk ends at NativeWindow.Callback, you certainly do have a threading problem.  You should see at least Main() and the message loop on the stack.  Carefully review your code yet again.  Good luck with it.

    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Tuesday, July 29, 2008 10:35 AM
    Wednesday, July 23, 2008 3:50 PM