Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to delete menuItems and their Handlers?

Status
Not open for further replies.

jonwondering

Programmer
May 11, 2005
13
US
Hey everybody, I have a little problem and I can't figure it out. I got a memory leak... Let's say I have a txt file with the following values in there:

Red
Green
Blue

My program reads in those values every 10 seconds and stores them as a menu in a ContextMenu control. I have done that with a timer (obviously) and everytime before it loads the items in a menu, it clears the menu (so that the menuitems would not pile up).

Here's the code inside the loop:

Code:
Dim Z As MenuItem
While ("... Not the end of file ...")
            ' Here 'Temp' stores the name of the color
            Z = New MenuItem(Temp)
            Me.ContextMenu1.MenuItems.Add(Z)
            AddHandler Z.Click, AddressOf Implement_Click
End While

Private Sub Implement_Click(ByVal Sender As Object, ByVal e As EventArgs) Handles Z.Click
            MsgBox("You clicked here")
End Sub



The problem is that every time the loop executes, it creates new MenuItems and new Handlers for them, making the memory usage of the program increase...

Does anybody know how I can delete those MenuItems and their Handlers to free up that space?
(The problem is that they are created dynamically...)

Thanks,
Jon
 
How do you clear the menu (do you use Me.ContextMenu1.MenuItems.Remove and remove each item)?

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
If you remove the items and destroy any references to them that you may have used, then the garbage collector will take care of freeing up the memory when it runs. If you are impatient you could always force the GC to collect! e.g.
Code:
        GC.Collect()
        GC.WaitForPendingFinalizers()

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
That's what I just tried to do. Before the program reads in the values (the colors), the size (memory usage) is 16,000. Right after it reads in the values the size jumps up to 16,700. And after it reads in the following code, the memory does not do anything but stay the same:

Code:
        RemoveHandler Me.ContextMenu1.MenuItems.Item(2).Click, AddressOf Implement_Click
        Me.ContextMenu1.MenuItems.RemoveAt(2)
        RemoveHandler Me.ContextMenu1.MenuItems.Item(1).Click, AddressOf Implement_Click
        Me.ContextMenu1.MenuItems.RemoveAt(1)
        RemoveHandler Me.ContextMenu1.MenuItems.Item(0).Click, AddressOf Implement_Click
        Me.ContextMenu1.MenuItems.RemoveAt(0)

        GC.Collect()
        GC.WaitForPendingFinalizers()

I am trying to delete the handlers too, theoretically they should occupy some space for each menuitem. I don't know if I am doing it right though...
Any idea?
 
Is it that big a problem to jump to 16,700? That is a fairly small amount of memory usage.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Nah, just extra 700kb is no problem, but my commands were in a timer because the list had to be constantly updated so every time it would add up 700kb... I think I found the problem though - it was a few other variables messing me up (as well as those menuitems with their handlers that we got rid of). I really appreciate your help though... You gave me some good ideas!

Thank you,
Jon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top