ContextMenuStrip is never disposed  
Author Message
Florida-her-akane





PostPosted: Mon Jan 09 05:30:02 CST 2006 Top

Winforms >> ContextMenuStrip is never disposed

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 ?

DotNet335  
 
 
AMercer





PostPosted: Mon Jan 09 05:30:02 CST 2006 Top

Winforms >> ContextMenuStrip is never disposed > 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.
 
 
Chris





PostPosted: Mon Jan 09 09:08:04 CST 2006 Top

Winforms >> ContextMenuStrip is never disposed


>> 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.

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

Chris


 
 
AMercer





PostPosted: Mon Jan 09 10:01:02 CST 2006 Top

Winforms >> ContextMenuStrip is never disposed > 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.

 
 
Chris





PostPosted: Mon Jan 09 12:39:40 CST 2006 Top

Winforms >> ContextMenuStrip is never disposed


>> 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.
>

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