Environnement:
- OS: Windows 10
- Visual Studio : VS 2019 Up to date Application: Full
- .NET 5.0
- VB.NET with some third part libraries
Situation:
I got an exception having came from nowhere on a toolstrip combobox add item method (first cmbFontStyle.Items.Add
):
Public Class MyForm
Private FontStyleTitre1 As Font = New Font(New FontFamily("Lato"), 18, FontStyle.Bold, 3)
Private FontStyleTitre2 As Font = New Font(New FontFamily("Lato"), 16, FontStyle.Underline Or FontStyle.Bold, 3)
Private FontStyleTitre3 As Font = New Font(New FontFamily("Lato"), 14, FontStyle.Underline Or FontStyle.Bold, 3)
Private FontStyleTitre4 As Font = New Font(New FontFamily("Lato"), 12, FontStyle.Underline, 3)
Private FontStyleCorpsdeTexte As Font = New Font(New FontFamily("Lato"), 10, FontStyle.Regular, 3)
Private Sub frmCorpusManagement_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cmbFontStyle.ComboBox.DrawMode = DrawMode.OwnerDrawVariable
AddHandler cmbFontStyle.ComboBox.DrawItem, AddressOf cmbFontStyle_DrawItem
AddHandler cmbFontStyle.ComboBox.MeasureItem, AddressOf cmbFontStyle_MeasureItem
LoadcmbFontStyle()
End Sub
Private Sub LoadcmbFontStyle()
Dim strMsgException As String = "Erreur dans le module LoadcmbFontStyle" & vbCrLf & "Impossible d'afficher la liste des corpus documentaires " & vbCrLf & vbCrLf & strMsgErrorTellYourAdmin & vbCrLf & vbCrLf & "-- Message d'erreur: "
Dim itemCorpusFontStyleTitre1 As New CorpusFontStyleItem With {.strTextStyleName = "Titre 1", .fontTextStyle = FontStyleTitre1}
Dim itemCorpusFontStyleTitre2 As New CorpusFontStyleItem With {.strTextStyleName = "Titre 2", .fontTextStyle = FontStyleTitre2}
Dim itemCorpusFontStyleTitre3 As New CorpusFontStyleItem With {.strTextStyleName = "Titre 3", .fontTextStyle = FontStyleTitre3}
Dim itemCorpusFontStyleTitre4 As New CorpusFontStyleItem With {.strTextStyleName = "Titre 4", .fontTextStyle = FontStyleTitre4}
Dim itemCorpusFontStyleCorps As New CorpusFontStyleItem With {.strTextStyleName = "Corps de Texte", .fontTextStyle = FontStyleCorpsdeTexte}
Try
cmbFontStyle.Items.Add(itemCorpusFontStyleTitre1)
cmbFontStyle.Items.Add(itemCorpusFontStyleTitre2)
cmbFontStyle.Items.Add(itemCorpusFontStyleTitre3)
cmbFontStyle.Items.Add(itemCorpusFontStyleTitre4)
cmbFontStyle.Items.Add(itemCorpusFontStyleCorps)
Catch ex As Exception
MsgBox(strMsgException & ex.Message, MsgBoxStyle.Critical, strMsgBoxCorpusDocTitleError)
Exit Try
End Try
End Sub
End Class
I guess it's some of these current "cryptic" issues.
Some links:
- VB.Net in Visual Studio 2015 - "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
- Programs randomly getting System.AccessViolationException
- VB.net - Get error "Attempted to read or write protected memory..." since provider changed
- Windows Forms ComboBox causing intermittent System.AccessViolationException
Resolution:
I tried:
- adding a new environment variable: doesn't do anything
- run VS as administrator: doesn't do anything
- add a try/catch: doesn't do anything
- uncheck the "Suppress JIT optimization on module load" option (Tools/Options/Debugging/General): doesn't do anything.
- switch the DropDownStyle between DropDown and DropDownList: doesn't do anything
I did not try:
- tweaking the CPU options
What it worked the best was to add cmbFontStyle.Items.Clear()
before the first cmbFontStyle.Items.Add
It works but not every time.
I don't get it. If anyone has suggestions or ideas...
EDIT: Following Jimi advices:
Option Strict On
Imports MySql.Data.Types
Imports MySql.Data.MySqlClient
Imports System.Text.RegularExpressions
Imports System.Text
Imports System.IO
Imports System.ComponentModel
Imports System.Drawing
Public Class frmCorpusManagement
Private WithEvents frmCorpusAddObjectNew As frmCorpusAddObject
Private frmWaitingCorpus As New frmTechWaiting
Private objectToAdd As FormatedObjectForCorpus
Private blnLoadInProgress As Boolean
Private graphicUnitUsed As GraphicsUnit = GraphicsUnit.Point
Private intcmbFontStyleTotalHeight As Integer
Private FontStyleTitre1 As Font = New Font(New FontFamily("Lato"), 18, FontStyle.Bold, graphicUnitUsed)
Private FontStyleTitre2 As Font = New Font(New FontFamily("Lato"), 16, FontStyle.Underline Or FontStyle.Bold, graphicUnitUsed)
Private FontStyleTitre3 As Font = New Font(New FontFamily("Lato"), 14, FontStyle.Underline Or FontStyle.Bold, graphicUnitUsed)
Private FontStyleTitre4 As Font = New Font(New FontFamily("Lato"), 12, FontStyle.Underline, graphicUnitUsed)
Private FontStyleCorpsdeTexte As Font = New Font(New FontFamily("Lato"), 10, FontStyle.Regular, graphicUnitUsed)
Private FontStyleForDisplay As Font = New Font(New FontFamily("Lato"), 9, FontStyle.Regular, graphicUnitUsed)
Private Sub frmCorpusManagement_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cmbFontStyle.ComboBox.DrawMode = DrawMode.OwnerDrawVariable
AddHandler cmbFontStyle.ComboBox.DrawItem, AddressOf cmbFontStyle_DrawItem
AddHandler cmbFontStyle.ComboBox.MeasureItem, AddressOf cmbFontStyle_MeasureItem
LoadcmbFontStyle()
cmbFontStyle.SelectedIndex = cmbFontStyle.Items.Count - 1
End Sub
Private Sub frmCorpusAddObject_evtObjectLinkFormated(sender As Object, e As EventArgs) Handles frmCorpusAddObjectNew.evtObjectLinkFormated
Dim objectToAddFormated As New FormatedObjectForCorpus
Dim strMsgException As String = "Erreur dans le Module frmCorpusAddObject_evtObjectLinkFormated" & vbCrLf & strMsgErrorTellYourAdmin & vbCrLf & vbCrLf & "-- Message d'erreur: "
Try
objectToAddFormated = frmCorpusAddObjectNew.objectToAddFormated
rtxtCorpusContent.InsertLink(objectToAddFormated.strText, objectToAddFormated.strLink)
Me.frmCorpusAddObjectNew.Close()
Exit Try
Catch ex As Exception
MsgBox(strMsgException & ex.Message, MsgBoxStyle.Critical, strMsgBoxCorpusDocTitle)
End Try
End Sub
Private Sub cmbFontStyle_MeasureItem(ByVal sender As Object, ByVal e As MeasureItemEventArgs)
Select Case e.Index
Case 0
e.ItemHeight = CInt(FontStyleTitre1.Height)
intcmbFontStyleTotalHeight += e.ItemHeight
Case 1
e.ItemHeight = CInt(FontStyleTitre2.Height)
intcmbFontStyleTotalHeight += e.ItemHeight
Case 2
e.ItemHeight = CInt(FontStyleTitre3.Height)
intcmbFontStyleTotalHeight += e.ItemHeight
Case 3
e.ItemHeight = CInt(FontStyleTitre4.Height)
intcmbFontStyleTotalHeight += e.ItemHeight
Case 4
e.ItemHeight = CInt(FontStyleCorpsdeTexte.Height)
intcmbFontStyleTotalHeight += e.ItemHeight
End Select
cmbFontStyle.DropDownHeight = intcmbFontStyleTotalHeight
End Sub
Private Sub cmbFontStyle_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs)
e.DrawBackground()
Dim myItem As CorpusFontStyleItem = DirectCast(cmbFontStyle.Items(e.Index), CorpusFontStyleItem)
e.Graphics.DrawString(myItem.strTextStyleName, myItem.fontTextStyle, New SolidBrush(Color.Black), e.Bounds)
e.DrawFocusRectangle()
End Sub
Private Sub LoadcmbFontStyle()
Dim strMsgException As String = "Erreur dans le module LoadcmbFontStyle" & vbCrLf & "Impossible d'afficher la liste des corpus documentaires " & vbCrLf & vbCrLf & strMsgErrorTellYourAdmin & vbCrLf & vbCrLf & "-- Message d'erreur: "
Dim lstComboFontStyle As New List(Of CorpusFontStyleItem)
Try
lstComboFontStyle.Add(New CorpusFontStyleItem With {.strTextStyleName = "Titre 1", .fontTextStyle = FontStyleTitre1})
lstComboFontStyle.Add(New CorpusFontStyleItem With {.strTextStyleName = "Titre 2", .fontTextStyle = FontStyleTitre2})
lstComboFontStyle.Add(New CorpusFontStyleItem With {.strTextStyleName = "Titre 3", .fontTextStyle = FontStyleTitre3})
lstComboFontStyle.Add(New CorpusFontStyleItem With {.strTextStyleName = "Titre 4", .fontTextStyle = FontStyleTitre4})
lstComboFontStyle.Add(New CorpusFontStyleItem With {.strTextStyleName = "Corps de Texte", .fontTextStyle = FontStyleCorpsdeTexte})
With Me.cmbFontStyle.ComboBox
.DisplayMember = "strTextStyleName"
.ValueMember = "strTextStyleName"
.DataSource = lstComboFontStyle
End With
Catch ex As Exception
MsgBox(strMsgException & ex.Message, MsgBoxStyle.Critical, strMsgBoxCorpusDocTitleError)
Exit Try
End Try
End Sub