Declaring an Event in a class leaved Memory Leak when class is instantiated and then disposed?

I think this one is for gurus. I am creating a class with an Event, then I instantiate the class and dispose of it. VB seems to be wasting 16 bytes each time. Any help I'm desperate!!

Example Class:

Public Class Myclass

Public Event Status(ByVal pText As String)

Public Sub New()
End Sub

Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class

Example Form That Instantiates / Clears the class

This form only requires a textbox (multiline / accepts return) and a button.
Imports System.Threading

Public Class Form3

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim LClass As New LeakingClass
LClass = Nothing
End Sub

Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim t As New Thread(AddressOf Launch)
t.SetApartmentState(ApartmentState.STA)
t.Start()
End Sub

Public Sub Launch()
While True
GC.Collect()
Me.Status(GC.GetTotalMemory(True))
Application.DoEvents()
Thread.Sleep(500)
End While
End Sub

Private Delegate Sub StatusCallBack(ByVal pText As String)
Public Sub Status(ByVal pText As String)
If Me.InvokeRequired Then
Dim d As New StatusCallBack(AddressOf Status)
Me.Invoke(d, New String() {pText})
Return
End If
If Me.TextBox1.Text.Length = 0 Then Me.TextBox1.Text = "START"
Me.TextBox1.SelectionStart = Me.TextBox1.Text.Length
Dim MyStart As Long = Me.TextBox1.Text.Length - 50
If MyStart < 1 Then MyStart = 1

Me.TextBox1.Text = Mid(Me.TextBox1.Text, MyStart)
Me.TextBox1.Text = Me.TextBox1.Text + Chr(13) + Chr(10) + pText
Me.TextBox1.SelectionLength = 0
Me.TextBox1.ScrollToCaret()
End Sub

End Class





Answer this question

Declaring an Event in a class leaved Memory Leak when class is instantiated and then disposed?

  • CharlieRussell

    You'll see this when you run the program in the IDE with the debugger. The debugger uses the managed heap as well. Run the release build outside of the IDE to do any memory leakage test.



  • pyeung

    Take a look at: http://support.microsoft.com/ kbid=919481. The issue is described in the KB article with more details.

    Hope this helps,



  • Kenny1815

    Thanks both of you for the excellent tips.



  • Declaring an Event in a class leaved Memory Leak when class is instantiated and then disposed?