R – AccessViolationException from a combo: Attempted to read or write protected memory

access-violationcomboboxwinforms

Users are occassionally getting the above error when using our application (VB.Net, Winforms, using v2 of the framework). I'm not able to reproduce it. The callstack is as follows:

: 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.ComboBox.WndProc(Message& m)
at ControlEx.AutoCompleteCombo.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 code for ControlEx.AutoCompleteCombo.WndProc is as follows:

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    Try
        If Not m_fReadOnly Then
            MyBase.WndProc(m)
        Else
            Select Case m.Msg
                Case WM_LBUTTONDOWN, WM_LBUTTONDBLCLK
                    ' do nothing
                Case Else
                    MyBase.WndProc(m)
            End Select
        End If
    Catch ex As OutOfMemoryException
        Throw New OutOfMemoryException("Exception during WndProc for combo " & Me.Name, ex)
    End Try
End Sub

The error handling was added so we can determine which combo causes the problem when we get an OutOfMemoryException.

Any clues as to what causes this would be muchly appreciated! 🙂

Best Answer

I have a strange non-deterministic feeling with the OutOfMemoryException in your code.

Why do you need that? And if you need it, may this be the cause of your problems? OutOfMemoryExceptions are very rare. If you have these, I would think it is a strong indication something else is wrong.

Related Topic