代码之家  ›  专栏  ›  技术社区  ›  ptutt

试图读取或写入受保护的内存。这通常表示其他内存已损坏

  •  3
  • ptutt  · 技术社区  · 16 年前

    我使用以下方法创建了从combobox继承的Winforms AutoComplete combobox控件:

    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
    

    组合框项目基于将组合框中的当前文本用作

    第一次调用时一切正常,但第二次调用时出现“尝试读取或写入受保护内存。这通常表示其他内存已损坏”错误。此错误在更新组合框的所有代码完成后出现。我得到以下堆栈跟踪:

    在 System.Windows.Forms.UnsafentiveMethods.DispatchMessageW(MSG& 味精)在 pvLoopData)在 原因(应用程序上下文上下文)
    在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
    在 System.Windows.Forms.Application.Run(ApplicationContext Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() 在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(字符串[]) 命令行)在 ApplicationShell.My.MyApplication.Main(字符串[]) Args)在 17d14f5c-a337-4978-8281-53493378c1071。vb:线路 81在 System.AppDomain.\u下一个安全组件(程序集 System.AppDomain.ExecuteAssembly(字符串 档案、证据 assemblySecurity,字符串[]args)位于 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() System.Threading.ThreadHelper.ThreadStart\u上下文(对象 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态)在 System.Threading.ThreadHelper.ThreadStart()

    有人能给我指出解决这个问题的正确方向吗?

    2 回复  |  直到 16 年前
        1
  •  4
  •   James    16 年前

    当我遇到这个错误时,事实证明这确实是一个取消引用空指针的问题,但它还是进行了方法调用,并且在尝试访问其中一个成员变量之前没有崩溃。在.Net出现之前,我就没有真正使用过VB,但您的堆栈跟踪表明它在某个本地调用中消亡了。如果是这样的话,您可能会遇到同样的问题,在这种情况下,我建议您仔细检查这些指针,不仅是在哪里崩溃,而且还要再上几层。

        2
  •  0
  •   Community CDub    8 年前

    将对象添加到items集合中,其ToString()方法不返回任何结果,这是一种简单的方法。可能还有其他方法可以达到相同的行为。

    继续诊断它,就像它是NullReferenceException一样。

    看见 What's the strangest corner case you've seen in C# or .NET? 另一个例子。

    推荐文章