views:

257

answers:

2

Hi, I've been having trouble with memory leaks with the SWF-ToolStrip. According to this http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=115600# is has been resolved. But here it seemes not.

Anyone know how to resolve this?

+1  A: 

This is a remarkably persistent complaint. The source of the leak was ToolStrip installing an event handler for the SystemEvents.UserPreferenceChanged event. So that it can respond to the user changing the theme or color scheme and redraw itself. This is a static event, forgetting to unregister the event handler will permanently leak the ToolStrip instance.

The bug has definitely been fixed in .NET 3.5 SP1. The ToolStrip.Dispose() method unregisters the event handler. If that's the version you are running, make sure that the Dispose() method indeed runs. A common mistake is to use Controls.Remove() to remove a control from a form but then forgetting to call Dispose() on the removed control.

Hans Passant
A: 

Private Sub frmBase_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed

    ' .NET BUG WORKAROUND
    ' MANUALLY DISPOSE OF ToolStip, MenuStrip and StatusStrip to release memory being held
    Dim aNames As New ArrayList
    Dim count As Integer = 0

    For Each oItem As ToolStripItem In Me.MenuStrip1.Items
        aNames.Add(oItem.Name)
    Next

    For i As Integer = 0 To aNames.Count - 1
        For Each oItem As ToolStripItem In Me.MenuStrip1.Items
            If oItem.Name = aNames(i) Then
                oItem.Dispose()
                Exit For
            End If
        Next
    Next

    count = 0
    aNames.Clear()
    For Each oItem As ToolStripItem In Me.ToolStrip1.Items
        aNames.Add(oItem.Name)
    Next

    For i As Integer = 0 To aNames.Count - 1
        For Each oItem As ToolStripItem In Me.ToolStrip1.Items
            If oItem.Name = aNames(i) Then
                oItem.Dispose()
                Exit For
            End If
        Next
    Next

    count = 0
    aNames.Clear()
    For Each oItem As ToolStripItem In Me.StatusStrip1.Items
        aNames.Add(oItem.Name)
    Next

    For i As Integer = 0 To aNames.Count - 1
        For Each oItem As ToolStripItem In Me.StatusStrip1.Items
            If oItem.Name = aNames(i) Then
                oItem.Dispose()
                Exit For
            End If
        Next
    Next

    Me.MenuStrip1.Dispose()
    Me.ToolStrip1.Dispose()
    Me.StatusStrip1.Dispose()

End Sub
Don Meuse