strange error/can ne one repoduce it?

  • Thread starter Thread starter Piedro
  • Start date Start date
P

Piedro

Can someone reproduce the following error?

I'm using the module at the bottom of my post to owner draw a menu
items, I call the module from a form like this:

Private Sub mnuOpen_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles mnuOpen.DrawItem
Dim Ic As New Icon(Application.StartupPath & "\101_72.ico")
DrawItems(e, mnuOpen, Ic)
End Sub

Private Sub mnuOpen_MeasureItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.MeasureItemEventArgs) Handles mnuOpen.MeasureItem
MeasureItems(e, mnuOpen)
End Sub


It al works just fine but if I try to debug and step through it, or if
I type something wrong so that I get an error message at runtime. The
next time I try to run it I keep getting this error message:
An unhandled exception of type 'System.OutOfMemoryException' occurred
in system.drawing.dll
Additional information: Out of memory.

and then right clicking (even on the desktop) doesn't work like it
should and I have to reboot my pc for everything to return to normal.
Is there something wrong in the module. Or isn't it my fault?

thanks a lot









'Module om icoontjes aan een menu toe te voegen

Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Imports System.Windows.Forms


Module IconsMenuMain

Dim m_Font As New Font("Arial", 8)

Sub MeasureItems(ByVal EvMeasureItem As
System.Windows.Forms.MeasureItemEventArgs, ByVal Mi As MenuItem)
Dim sf As StringFormat = New StringFormat()
sf.HotkeyPrefix = HotkeyPrefix.Show
sf.SetTabStops(60, New Single() {0})
EvMeasureItem.ItemHeight = 22
EvMeasureItem.ItemWidth =
CInt(EvMeasureItem.Graphics.MeasureString(GetRealText(Mi), _
m_Font,
10000, sf).Width) + 30
sf.Dispose()
sf = Nothing
End Sub

Sub DrawItems(ByVal EvDrawItems As
System.Windows.Forms.DrawItemEventArgs, ByVal Mi As MenuItem, ByVal
m_Icon As Icon)
Dim br As Brush
Dim br2 As Brush
Dim fDisposeBrush As Boolean
Dim rand As Pen

Dim rcBk As Rectangle = EvDrawItems.Bounds
rcBk.Height -= 1
rcBk.Width -= 1
If CBool(EvDrawItems.State And DrawItemState.Selected) Then
br = New LinearGradientBrush(rcBk, Color.MediumPurple,
Color.YellowGreen, 0)
fDisposeBrush = True
rand = New Pen(Color.DarkBlue, 1)
EvDrawItems.Graphics.FillRectangle(br, rcBk)
EvDrawItems.Graphics.DrawRectangle(rand, rcBk)

Else
br = SystemBrushes.Control
br2 = New SolidBrush(Color.WhiteSmoke)
rand = System.Drawing.SystemPens.Control
EvDrawItems.Graphics.DrawRectangle(rand, rcBk)
rcBk.X += 24
rcBk.Width -= 23
rcBk.Y -= 1
rcBk.Height += 2
EvDrawItems.Graphics.FillRectangle(br2, rcBk)
rcBk.X = 0
rcBk.Width = 24
EvDrawItems.Graphics.FillRectangle(br, rcBk)
br2.Dispose()

End If




'Dim rcIcon As New Rectangle(EvDrawItems.Bounds.Left + 2,
EvDrawItems.Bounds.Top + 2, m_Icon.Width, m_Icon.Height)
'EvDrawItems.Graphics.ExcludeClip(rcIcon)

If fDisposeBrush Then br.Dispose()
br = Nothing

Dim sf As StringFormat = New StringFormat()
sf.HotkeyPrefix = HotkeyPrefix.Show
sf.SetTabStops(60, New Single() {0})
If Mi.Enabled Then
br = New SolidBrush(EvDrawItems.ForeColor)
Else
br = New SolidBrush(Color.Gray)
End If


EvDrawItems.Graphics.DrawString(GetRealText(Mi), m_Font, br, _
EvDrawItems.Bounds.Left + 25,
_
EvDrawItems.Bounds.Top + 2,
sf)

br.Dispose()
br = Nothing
sf.Dispose()
sf = Nothing
If Not m_Icon Is Nothing Then
If Not Mi.Checked Then
EvDrawItems.Graphics.DrawIcon(m_Icon,
EvDrawItems.Bounds.Left + 2, _
EvDrawItems.Bounds.Top +
2)
Else
EvDrawItems.Graphics.DrawIcon(m_Icon,
EvDrawItems.Bounds.Left + 2, _
EvDrawItems.Bounds.Top +
2)

End If

End If
End Sub

Function GetRealText(ByVal Mi As MenuItem) As String
Dim s As String = Mi.Text
If Mi.ShowShortcut And Mi.Shortcut <> Shortcut.None Then
Dim k As Keys = CType(Mi.Shortcut, Keys)
s = s & Convert.ToChar(9) & _
TypeDescriptor.GetConverter(GetType(Keys)).ConvertToString(k)
End If
Return s
End Function

End Module
 
Good Evening Piedro,

Your problem is common to programmers. It is known as memory leak. You are
not handling your errors with try catch statements and when that happens your
computer still thinks it is running that program and will not release the
memory unless you reboot.

My suggestion is to put a try catch such as the one below this way when a
runtime error occurs it will release memory and you can at least debug
without running out of memory.
Try
'potential code that is having errors
Catch ex As Exception
MsgBox(ex.Message)
End
End Try

Rachel
 
Hi Rachel,

thnx for your reply, I know about memory leaks but the problem even
kicks in when there isn't an error in the prog, but when I just step
through it by placing a break in the code and then step through it,
that's why I think it's a strange error.

Grtz Peter
 
Back
Top