12

Some back story:

I have searched both stackoverflow and Google to figure out the origin of this particular exception.

We have a Windows Forms (C#, .NET 2.0) application that will occasionally throw a System.AccessViolationException (stack trace below). The exception has occurred on both Windows XP (SP3) and Windows Vista (SP2). From user input, we have pinned down that the exception occurs when the selected index of a ComboBox is changed.

The ComboBox is a standard Windows Forms component, and it is contained in a custom control that inherits System.Windows.Forms.UserControl.

My main query is:

Where does this exception come from?

  • Is it from some internal bug in the .NET framework?
  • Could it be code that is dispatched on the SelectedIndexChanged event that causes it?

Related topics on stackoverflow:

Stack trace:

Message: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Type: System.AccessViolationException

   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.WmCommand(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ComboBox.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)
Community
  • 1
  • 1
Christoffer
  • 2,125
  • 15
  • 21

2 Answers2

1

I had these problems as well, way back when I used .net 2.0 WinForms. I seem to remember Microsoft released a Hotfix for this. I googled it and found these two. You could try them.

http://support.microsoft.com/kb/923028

http://support.microsoft.com/kb/971030

Also, did you install all Service Packs for .NET 2.0?

Edwin de Koning
  • 14,209
  • 7
  • 56
  • 74
  • Thanks for the reply @rewinder. The second link might be of interest actually. However applying hotfixes/service packs is something we would avoid for as long as possible, having the application installed on hundreds of machines. I am currently investigating if the combobox is altered meanwhile the GUI-thread does some work on it - a rumor says the underlying implementation of the ComboBox is not fully thread safe. – Christoffer May 31 '11 at 13:15
  • @Christoffer: Ok. Well, let me know if you find anything. – Edwin de Koning May 31 '11 at 13:20
  • 1
    Found another somewhat related topic at Microsoft Support: http://support.microsoft.com/kb/952544 But I am not using any autocompletion features. However, items can be added to the Items collection OnKeyDown or OnSelectedIndexChanged. I don't know yet if this can cause the problem, I'll continue my investigation. – Christoffer Jun 01 '11 at 08:54
-1

Don't give it a chance to overload memory, dispose all your new and unused items/controls Do not constanly add/remove objects to a listbox, typically during thread operations.

GDI problems occur if you don't clear the memory.

Combobox exceptions occur, it may have something related to the selecteditem, whether that item has text or an integer stored. Text must be text, integer must be integer.