ContextMenuStrip is never disposed

G

Guest

I have created a sample Windows Forms Application, on mouse right click i
create a new ContextMenuStrip and shows it. I close the ContextMenuStrip by
clicking outside it or by clicking an item. Even after a GC.Collect call, the
ContextMenuStrip is never disposed.

Analysing with a .NET Memory Profiler i have noticed that, after closure,
the ContextMenuStrip is still alive : referenced by the ControlNativeWindow
and by SytemEvents handlers.

I have tried to call a Dispose in ContextMenuStrip.Closed handler, but it
raises an exception. Any idea ?
 
G

Guest

I have created a sample Windows Forms Application, on mouse right click i
create a new ContextMenuStrip and shows it. I close the ContextMenuStrip by
clicking outside it or by clicking an item. Even after a GC.Collect call, the
ContextMenuStrip is never disposed.

When you create, show, and dismiss a ContextMenu, you also have to dispose
it, otherwise you get a memory leak. I presume the same is true for a
ContextMenuStrip.

For a ContextMenu, the logic flow is as follows:
initialize a variable to capture the user's menu selection
write a menuitem click event handler that captures the user's selection
create the ContextMenu, each MenuItem having tied to the click handler
show the context menu
DoEvents ' necessary to let the click handler fire
dispose ' necessary else memory leak, safe because the menu is done

Good luck.
 
C

Chris

AMercer said:
When you create, show, and dismiss a ContextMenu, you also have to dispose
it, otherwise you get a memory leak. I presume the same is true for a
ContextMenuStrip.

For a ContextMenu, the logic flow is as follows:
initialize a variable to capture the user's menu selection
write a menuitem click event handler that captures the user's selection
create the ContextMenu, each MenuItem having tied to the click handler
show the context menu
DoEvents ' necessary to let the click handler fire
dispose ' necessary else memory leak, safe because the menu is done

Good luck.

When you create a context menu through VS 2005 IDE, do you still need to
manually dispose of it?

Chris
 
G

Guest

When you create a context menu through VS 2005 IDE, do you still need to
manually dispose of it?

No, if I understand the sense of your question. If you are going to reuse
the cm (ie the same cm gets used and reused as the user does his thing), then
dispose is not needed, and that is true if you create the cm via the IDE as
you suggest or via code.

The recipe I described is for a single use cm where you create it, use it,
and get rid of it. The second time around, you start from scratch and create
a new cm, use it, etc. In this scenario, you must dispose or you will leak
memory.
 
C

Chris

AMercer said:
No, if I understand the sense of your question. If you are going to reuse
the cm (ie the same cm gets used and reused as the user does his thing),
then
dispose is not needed, and that is true if you create the cm via the IDE
as
you suggest or via code.

The recipe I described is for a single use cm where you create it, use it,
and get rid of it. The second time around, you start from scratch and
create
a new cm, use it, etc. In this scenario, you must dispose or you will
leak
memory.

I thought this might be the case, but I'm new to WinForms, and only been
learning c# for the past month, so I'm still getting my bearings :).

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top