2009-01-08 39 views
3

我創建的Winforms自動完成組合框控制從組合框繼承以下方法的指示:試圖讀取或寫入受保護的內存。這通常是其他存儲器已損壞

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 

的「SetAutoCompleteMethod」是指向該填充 的方法的代表基於使用組合框中的當前文本作爲通配符搜索的前綴的組合框項目。這提供了與ajax自動完成組合框類似的功能。

這一切在第一次被調用時都可以正常工作,但第二次被調用時我得到了「嘗試讀取或寫入受保護的內存,這通常表示其他內存已損壞」錯誤。在所有更新組合框的代碼完成後出現錯誤。我得到以下堆棧跟蹤:

在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG & MSG)在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms。 UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(的Int32 dwComponentID,的Int32原因,的Int32 pvLoopData)在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(的Int32 原因,ApplicationContext的上下文)
在 System.Windows.Forms.Application .ThreadContext.RunMessageLoop(的Int32 原因,ApplicationContext的上下文)
在 System.Windows.Forms.Application.Run在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() (ApplicationContext的 上下文)在 Microsoft.Vi sualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() 在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(字符串[] COMMANDLINE)在 ApplicationShell.My.MyApplication.Main(字符串[] Args)來在 17d14f5c-a337- 4978-8281-53493378c1071.vb:線 System.AppDomain._nExecuteAssembly(大會 組件,字串[] args)在 System.AppDomain.ExecuteAssembly(字符串 assemblyFile,證據 assemblySecurity,字串[] args)在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(對象 狀態)在 System.Threading.ExecutionContext.Run(的ExecutionContext 的ExecutionContext,ContextCallback 回調,對象狀態)在 System.Threading.ThreadHelper.ThreadStart()

任何人都可以指出我正確的方向來解決這個問題嗎?

+0

我相信這與數據源有關。在加載新項目之前,我將組合框的數據源設置爲「無」,並且發生錯誤的頻率較低? – ptutt 2010-05-08 06:49:24

回答

4

當我得到這個錯誤時,它實際上是一個取消引用空指針的問題,但它仍然調用方法,直到它試圖訪問其中一個成員變量時纔會崩潰。自從.Net之前,我並沒有真正使用過VB,但是你的堆棧跟蹤表明它在某個地方的本地調用中死了。如果是這樣的話,你可能會遇到同樣的問題,在這種情況下,我建議你仔細檢查這些指針,不僅僅是它崩潰的地方,還有一些關卡。

相關問題