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

问题描述 投票:0回答:1

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

我有一个维护程序,多年来我一直在成功使用以下功能,直到我们最近从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, BoldFont, sbBlackBrush, HeaderRect, sf)
                sbBlackBrush.Dispose()
            Case Else
                dicImages("Tab" & e.Index) = ilTabIconsForDocumentScanInstances.Images(IconsForDocumentScanInstances.DocumentExists)
                e.Graphics.DrawString(SelectedTab.Text, BoldFont, sbBlackBrush, HeaderRect, 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", "Delete Client Letter", "Excessive Documentation"
                dicImages("Tab" & e.Index) = ilTabIconsForDocumentScanInstances.Images(IconsForDocumentScanInstances.DeleteDocument)
                e.Graphics.DrawString(SelectedTab.Text, e.Font, sbRedBrush, HeaderRect, sf)
                sbRedBrush.Dispose()
            Case "No Documentation", "Missing Documentation"
                dicImages("Tab" & e.Index) = ilTabIconsForDocumentScanInstances.Images(IconsForDocumentScanInstances.NoDocumentExists)
                e.Graphics.DrawString(SelectedTab.Text, e.Font, sbBlackBrush, HeaderRect, sf)
                sbBlackBrush.Dispose()
            Case Else
                dicImages("Tab" & e.Index) = ilTabIconsForDocumentScanInstances.Images(IconsForDocumentScanInstances.DocumentExists)
                e.Graphics.DrawString(SelectedTab.Text, e.Font, sbBlackBrush, HeaderRect, sf)
                sbBlackBrush.Dispose()
        End Select

        e.Graphics.DrawImage(dicImages("Tab" & e.Index), intIconPositionX, 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, together, are marked for deletion.

            btnMarkCurrentDocumentForDeletion.Enabled = False
            btnUnmarkCurrentDocumentForDeletion.Enabled = False

        End If

    Else 'the coversheet and all associated documents, together, are 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

    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 catalog”并重新启动

  • 将“平台目标”从“任何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

我感谢您的投入。

.net vb.net memory access-violation protected
1个回答
0
投票

我可以建议您以主管部门的身份打开项目:

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

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

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

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

正如您提到的

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

© www.soinside.com 2019 - 2024. All rights reserved.