title: Attempted to read or write protected memory. This is often an indication that other memory has been corrupted
body: <p>I created a Winforms AutoComplete combobox control inheriting from ComboBox with the following method:</p>
  
  <pre><code>Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
          MyBase.OnKeyPress(e)
          Dim text As String = Me.Text
          If Char.IsLetter(e.KeyChar) Then
              text = text + e.KeyChar
          End If
          If (text.Length = AutoCompleteSearchChars And Char.IsLetter(e.KeyChar)) Then
              SetAutoCompleteMethod.Invoke(text)
              Me.AutoCompleteSource = AutoCompleteSource.ListItems
              Me.Text = text
              Me.SelectionStart = text.Length
              Me.SelectionLength = 0
              If Me.Items.Count &gt; 0 Then
                  Me.SelectedIndex = 0
              End If
              e.Handled = True
          End If
      End Sub
  </code></pre>
  
  <p>The "SetAutoCompleteMethod" is a delegate pointing to a method that populates the
  combobox items based on using the current text in the combo box as the prefix for a
  wildcard search.  This provides similar functionality to the ajax autocomplete combobox.</p>
  
  <p>This all works fine the first time it is called, but the second time it is called I get the "Attempted to read or write protected memory. This is often an indication that other memory has been corrupted" error.  The error appears after all the code to update the combobox is completed.  I get the following stack trace:</p>
  
  <blockquote>
    <p>at
    System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp;
    msg)    at
    System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.
    UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
    dwComponentID, Int32 reason, Int32
    pvLoopData)    at
    System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
    reason, ApplicationContext context)<br>
    at
    System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
    reason, ApplicationContext context)<br>
    at
    System.Windows.Forms.Application.Run(ApplicationContext
    context)    at
    Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
    at
    Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
    at
    Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[]
    commandLine)    at
    ApplicationShell.My.MyApplication.Main(String[]
    Args) in
    17d14f5c-a337-4978-8281-53493378c1071.vb:line
    81    at
    System.AppDomain._nExecuteAssembly(Assembly
    assembly, String[] args)    at
    System.AppDomain.ExecuteAssembly(String
    assemblyFile, Evidence
    assemblySecurity, String[] args)    at
    Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    at
    System.Threading.ThreadHelper.ThreadStart_Context(Object
    state)    at
    System.Threading.ExecutionContext.Run(ExecutionContext
    executionContext, ContextCallback
    callback, Object state)    at
    System.Threading.ThreadHelper.ThreadStart()</p>
  </blockquote>
  
  <p>Is Anyone able to point me in the right direction to solve this?</p>