I created a Winforms AutoComplete combobox control inheriting from ComboBox with the following method:
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 > 0 Then Me.SelectedIndex = 0 End If e.Handled = True End If End Sub
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.
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:
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& 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)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
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()
Is Anyone able to point me in the right direction to solve this?
When I’ve gotten that error, it turned out to really be an issue with dereferencing a null pointer, but it made the method call anyway and didn’t crash until it tried to access one of the member variables. I haven’t really worked with VB since before the days of .Net, but your stack trace suggests it died in a native call somewhere. If that’s the case, you might be running into the same issue in which case I’d recommend you double check those pointers, not just where it crashes, but a couple levels up too.