Visual Studio 2015中的VB.Net-“尝试读取或写入受保护的内存。这通常表明其他内存已损坏。”

每个人。我希望在这场大流行中一切都好。

我有一个维护程序,多年来我一直在成功使用以下功能,直到我们最近从Windows 7更改为Windows 10。

除其他外,我的程序还显示了扫描到Xerox Docushare中的PDF文档。这些文档与条形码中的参考ID相关联。一张封面(条形码)可能有一个或几十个扫描实例。

我有一个用户定义的控件(ucDocushare_DocumentSetByRefID.vb),该控件具有ListView(lvwDocuments)和TabControl(tcDocumentScanInstances)。功能是ListView显示代表扫描集的封面。当用户单击ListView中的项目时,将显示TabControl,其中包含与所选封面相关的每个单独扫描实例的选项卡。

每时每刻,没有明显的原因,当我单击ListView中的封面项目时,程序将直接终止。在Visual Studio 2015中进行调试时,显示以下消息。它与文档的大小或扫描实例的数量无关。我成功地打开了很多页面的大型文档。我也有许多成功扫描实例出现。

System.accessViolationException未处理 消息:System.Windows.Forms.dll中出现了“ System.accessViolationException”类型的未处理异常

其他信息:尝试读取或写入受保护的内存。这通常表明其他内存已损坏。

此外,“中断模式”选项卡将显示以下内容:

应用程序处于中断模式

您的应用已进入中断状态,但由于所有线程都在执行外部代码(通常是系统代码或框架代码),因此没有可显示的代码。

从此的“结束子行”开始时,立即发生错误:

Private Sub tcDocumentScanInstances_DrawItem(sender As Object,e As DrawItemEventArgs) _
    Handles tcDocumentScanInstances.DrawItem

此DrawItem子标签为标签标签做了一些字体管理,但主要决定了在这些标签上显示哪个图标。

这是该子代码的完整代码:

'Color code document tab labels and display appropriate icons.
 Private Sub tcDocumentScanInstances_DrawItem(sender As Object,e As DrawItemEventArgs) _
    Handles tcDocumentScanInstances.DrawItem

Try

    Dim intTabIndex As Integer = 0

    '  Identify which TabPage is currently selected
    Dim SelectedTab As TabPage = tcDocumentScanInstances.TabPages(e.Index)

    '  Get the area of the header of this TabPage.  This is the actual label for the tab page.
    Dim HeaderRect As Rectangle = tcDocumentScanInstances.GetTabRect(e.Index)

    '  Create a Brush to paint the Text
    Dim sbBlackBrush As New SolidBrush(Color.Black)
    Dim sbRedBrush As New SolidBrush(Color.Red)

    '  Set the Alignment of the Text
    Dim sf As New StringFormat()
    sf.Alignment = StringAlignment.Center
    sf.LineAlignment = StringAlignment.Center

    '  Paint the Text using the appropriate Bold setting 
    Dim intIconPositionX As Integer = HeaderRect.Left + 4
    Dim intIconPositionY As Integer = HeaderRect.Top + 7

    Dim dicImages As New Dictionary(Of String,Image)()
    dicImages("Tab" & e.Index) = Nothing  ' Set the value of the "variable"

    tcDocumentScanInstances.Padding = New System.Drawing.Point(15,15)
    'tcDocumentScanInstances.TabPages(0).Width = 500

    If Convert.ToBoolean(e.State And DrawItemState.Selected) Then

        Dim BoldFont As New Font(tcDocumentScanInstances.Font.Name,tcDocumentScanInstances.Font.Size,FontStyle.Bold)

        e.Graphics.FillRectangle(New SolidBrush(systemcolors.ButtonFace),e.Bounds)

        If tcDocumentScanInstances.TabPages(e.Index).Tag Is Nothing Then
            tcDocumentScanInstances.TabPages(e.Index).Tag = ""
        End If

        Select Case tcDocumentScanInstances.TabPages(e.Index).Tag.ToString
            Case "Delete","Delete Client Letter","Excessive Documentation"
                dicImages("Tab" & e.Index) = ilTabIconsForDocumentScanInstances.Images(IconsForDocumentScanInstances.DeleteDocument)
                e.Graphics.DrawString(SelectedTab.Text,BoldFont,sbRedBrush,HeaderRect,sf)
                sbRedBrush.Dispose()
            Case "No Documentation"
                dicImages("Tab" & e.Index) = ilTabIconsForDocumentScanInstances.Images(IconsForDocumentScanInstances.NoDocumentExists)
                e.Graphics.DrawString(SelectedTab.Text,sbBlackBrush,sf)
                sbBlackBrush.Dispose()
            Case Else
                dicImages("Tab" & e.Index) = ilTabIconsForDocumentScanInstances.Images(IconsForDocumentScanInstances.DocumentExists)
                e.Graphics.DrawString(SelectedTab.Text,sf)
                sbBlackBrush.Dispose()
        End Select

        e.Graphics.DrawImage(dicImages("Tab" & e.Index),intIconPositionX,intIconPositionY)

    Else

        e.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(128,167,240)),e.Bounds)
        If tcDocumentScanInstances.TabPages(e.Index).Tag Is Nothing Then
            tcDocumentScanInstances.TabPages(e.Index).Tag = ""
        End If

        Select Case tcDocumentScanInstances.TabPages(e.Index).Tag.ToString
            Case "Delete",e.Font,sf)
                sbRedBrush.Dispose()
            Case "No Documentation","Missing Documentation"
                dicImages("Tab" & e.Index) = ilTabIconsForDocumentScanInstances.Images(IconsForDocumentScanInstances.NoDocumentExists)
                e.Graphics.DrawString(SelectedTab.Text,intIconPositionY)

    End If

    If tcDocumentScanInstances.SelectedTab.Tag Is Nothing Then
        tcDocumentScanInstances.SelectedTab.Tag = ""
    End If

    If frmCaseMaintenance.tcDocumentationByRefID.TabPages( _
        frmCaseMaintenance.tcDocumentationByRefID.SelectedIndex).Tag.ToString.Length >= "Delete".Length Then

        If frmCaseMaintenance.tcDocumentationByRefID.TabPages( _
            frmCaseMaintenance.tcDocumentationByRefID.SelectedIndex).Tag.ToString.Substring(0,"Delete".Length) <> "Delete" Then
            'The coversheet and all associated documents,together,are not marked for deletion.

            Select Case tcDocumentScanInstances.SelectedTab.Tag.ToString.Trim
                Case "Delete","Delete Client Letter"
                    btnmarkCurrentDocumentForDeletion.Enabled = False
                    btnUnmarkCurrentDocumentForDeletion.Enabled = True
                Case "No Documentation","Missing Documentation"
                    'A tab displaying a message that there is no documentation can not be deleted.
                    btnmarkCurrentDocumentForDeletion.Enabled = False
                    btnUnmarkCurrentDocumentForDeletion.Enabled = False
                Case Else
                    btnmarkCurrentDocumentForDeletion.Enabled = True
                    btnUnmarkCurrentDocumentForDeletion.Enabled = False
            End Select

        Else 'the coversheet and all associated documents,are marked for deletion.

            btnmarkCurrentDocumentForDeletion.Enabled = False
            btnUnmarkCurrentDocumentForDeletion.Enabled = False

        End If

    Else 'the coversheet and all associated documents,are marked for deletion.

            Select Case tcDocumentScanInstances.SelectedTab.Tag.ToString.Trim
                Case "Delete","Missing Documentation"
                    'A tab displaying a message that there is no documentation can not be deleted.
                    btnmarkCurrentDocumentForDeletion.Enabled = False
                    btnUnmarkCurrentDocumentForDeletion.Enabled = False
                Case Else
                    btnmarkCurrentDocumentForDeletion.Enabled = True
                    btnUnmarkCurrentDocumentForDeletion.Enabled = False
            End Select

    End If

Catch ex As Exception

    If Err.Description = "A generic error occurred in GDI+." Then
        'This error probably was tripped by this line:  e.Graphics.FillRectangle(New SolidBrush(systemcolors.ButtonFace),e.Bounds)
        'tabDocumentScanInstance_DrawItem() will execute again without that line causing a problem,so we do nothing here.
    Else
            MessageBox.Show( _
                 "Class Name:  " & Me.Name & vbCrLf & _
                 "Sub Name:  tcDocumentScanInstances_DrawItem()" & vbCrLf & _
                 "Error Number:  " & Err.Number & vbCrLf & _
                 "Message:  " & Err.Description,_
                 gstrExecutableName & " - Error",_
                 MessageBoxButtons.OK,MessageBoxIcon.Error)
    End If

End Try

End Sub

正在使用的产品

microsoft Visual Studio Professional 2015 版本14.0.25431.01更新3

microsoft .NET Framework 版本4.8.03752

尝试的解决方案

  • 重新启动计算机

  • 删除了可执行文件并生成了一个新文件

  • 在命令提示符中执行“ netsh winsock reset”并重新启动;还在命令提示符下执行了“ netsh winsock reset 目录”并重新启动

  • 将“平台目标”从“任何CPU”更改为“ x86”。

    1. [项目名称]属性->“编译”选项卡->“编译选项”->“目标CPU”
    2. 重建/构建项目。 (当“ x86”无法解决问题时,我将其更改回“任何CPU”。)
  • 建议不要在Visual Studio中选中以下内容。已经是了。

       Tools menu ->Options -> Debugging -> General -> Uncheck this option "Suppress JIT optimization on module load"
    
  • 使用其他文件副本(相同的日期/时间和文件大小)替换了位于此处的System.Windows.Forms.dll:C:\ Windows \ microsoft.NET \ Framework \ v2.0.50727

我感谢您的投入。

iCMS 回答:Visual Studio 2015中的VB.Net-“尝试读取或写入受保护的内存。这通常表明其他内存已损坏。”

我通过暴力解决方案成功。我发现如果从ListView中选择麻烦的项目而没有 first 从ListView中选择不是会导致问题的其他任何项目,则会发生问题。因此,解决方案是以某种方式选择一个不会引起问题的项目,然后再选择一个不会造成问题的项目。我怎么知道我选择哪一个不会引起问题呢?另外,如果列表中只有一项,那我该怎么办?

解决方案是创建一个虚拟文档并始终首先加载它。

和以前一样,用户选择一个填充ListView(lvwDocuments)的类别,该类别显示代表扫描集的封面。但是,现在,在将这些封面加载到列表中之前,将加载一个代表虚拟封面的项目。 (列表中的第一项是虚拟封面,其余为所选类别的有效项。)基于该虚拟项作为列表中的第一项,我将虚拟文档加载到tabControl(tcDocumentScanInstances)中的选项卡上。我从ListView中(lvwDocuments)删除所述伪盖板项并隐藏标签上的伪文档与面板显示一个消息,告诉用户在选择了盖板项扫描实例将出现在标签。 (令人惊讶的是,它实际上比我以前使用的界面看起来更好,并且消息似乎也没有不必要或不合适!)由于它是如此之快地被加载和删除,因此用户从未在列表中看到伪项目。

那里有。从我在寻找解决方案时发现的结果来看,似乎有很多不同的情况发生了此错误。我希望我知道更好的方法来防止这种情况发生,也可以将其应用于其他情况。在这种情况下,此解决方案对我有用。我希望它能以某种方式帮助某人。

,

我可以建议您以管理员身份打开项目:

右键单击VS2015->以管理员身份运行,然后打开您的项目。

调试代码。我认为您的问题不在于代码,而在于访问资源。它说here

您可以像平常一样在Visual Studio IDE中执行几乎所有操作 用户,但是您需要管理员权限才能完成 以下任务:

工具箱||将经典的COM控件添加到工具箱。 ||使用 工具箱

您提到的

'我有一个用户定义的控件(ucDocushare_DocumentSetByRefID.vb)'

本文链接:https://www.f2er.com/2251712.html

大家都在问