How to dispose ContextMenus

  • Thread starter Thread starter Michael Sander
  • Start date Start date
M

Michael Sander

Hi,
I found out, that a simple Form, created only with the designer, wont
Dispose its contextmenu if you show the form, right-click it, and dispose
the form afterwards.
This isnt really what I expected and causes some huge memory-leaks in my
app.

All works fine, if I add the ContextMenu to the form.components manually.
(funny, the designer tells me not to modify the InitializeComponent(), but
since it works so stupid, I'll have to)

To me this doens't look like how it's meant to be, so can somebady please
tell me how it should work?

Regards,
Michael Sander
 
Michael,

..NET 2.0 context menu strips are added to the form's components collection,
thus disposed along with the form.
..NET1.x context menus are not, but is it a big deal? How manu context menus
do you have to worry about? Thei will be eventually disposed by the
finalizer.
If you want them disposed, though, don't change the InitializeComponent
method. In the constructor after the call to this method add a line(s) to
add this components to the form
s *componets* collection.
 
Thanks, that helps.

As for the big deal: i got overriden Components all over wich refer objects
like lists, records, fields and commands, all pending on each other. Thus an
not freed ContextMenu means not freeing tons of data.

Now I'll have to check with every component if the desinger works correct or
not :(
 
Michael,

Dispose is meant to be used for releasing unmanaged resources. It has noting
to do with managed ones. That is when you call dispose you don't free the
memory in the managed heap. That memory will become free when the GC
happens. If you keep references to managed objects you don't need to worry
about thinks like setting references to null for example.
For example if you have object A that references objects B and C you don't
need to implement Dispose in A that sets the reference to B and C to null.
As long as A is not referenced any more and there is no other references to
B and C, but only the one that A holds, the three of them will be garbage
collected when GC kicks off.

This is just a remark.
 
yeah, i now.

But it just doesnt works with that ContextMenu. Alle Objects beeing
referenced are managed, the GC.Collect is called manually a couple of time
for testing, no references from outside go to that silly Menu.
So in this case I or better the containing form MUST call
ContextMenu.Dispose.
Odd enough.

Thanks for your help,
Michael Sander
 
Michael said:
Hi,
I found out, that a simple Form, created only with the designer, wont
Dispose its contextmenu if you show the form, right-click it, and dispose
the form afterwards.
This isnt really what I expected and causes some huge memory-leaks in my
app.

All works fine, if I add the ContextMenu to the form.components manually.
(funny, the designer tells me not to modify the InitializeComponent(), but
since it works so stupid, I'll have to)

To me this doens't look like how it's meant to be, so can somebady please
tell me how it should work?

In case you missed my post in your last thread:

<http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/msg/764815931aa14ff9>
<http://tinyurl.com/frdsv>

In shot: This looks like it was an 'oversight' in 1.1; it's fixed in
2.0.
 
Back
Top