VB.Net 在 Visual Studio 2015 - "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

VB.Net in Visual Studio 2015 - "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

各位。希望疫情期间一切都好。

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

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

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

有时,当我单击 ListView 中的封面项目时,无缘无故地,程序会直接终止。在 Visual Studio 2015 调试时,显示以下消息。它与文档的大小或扫描实例的数量无关。我已经成功地提出了很多页的非常大的文档。我也有很多扫描实例成功出现。

System.AccessViolationException was unhandled Message: An unhandled exception of type 'System.AccessViolationException' occurred in System.Windows.Forms.dll

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

此外,还会出现一个中断模式选项卡,其中包含以下内容:

The application is in break mode

Your app has entered a break state, but there is no code to show because all threads were executing external code (typically system or framework code).

从以下的 End Sub 行开始时立即出现错误:

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

正在使用的产品

微软 Visual Studio 专业版 2015 版本 14.0.25431.01 更新 3

微软 .NET 框架 版本 4.8.03752

尝试过的解决方案

感谢您的意见。

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

Right click VS2015-> Run as Administrator and Open your project.

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

You can do nearly everything in the Visual Studio IDE as a normal user, but, you need administrator permissions to complete the following tasks:

Toolbox || Adding classic COM controls to the Toolbox. || Using the Toolbox

如你所说

'I have a user defined control (ucDocushare_DocumentSetByRefID.vb)'

我通过暴力破解成功了。我发现如果我 select 从 ListView 中编辑麻烦的项目而没有 首先 select 从 ListView 中编辑任何其他项目 没有导致问题。因此,解决方案是以某种方式 select 一个没有导致问题的项目,然后 selecting 一个导致问题的项目。我怎么知道我可以 select 哪一个不会导致问题?另外,如果列表中只有一项,那我该怎么办?

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

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

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