Context Menu Strip issue

T

Tim Kelley

I am using a context menu strip in my program (mdi). I populate the options
with the following code:

cms.Items.Add("Select this Image");

cms.Items[0].Tag = "Select";

cms.Items[0].Image = Image.FromFile("Checkbx.bmp");

cms.Items.Add("Un-Select All Images");

cms.Items[1].Tag = "UnSelect";

cms.Items[1].Image = Image.FromFile("Unchecked.bmp");

cms.Items.Add("-");

cms.Items[2].Tag = "";

cms.Items.Add("Reject this Image");

cms.Items[3].Tag = "Reject";

cms.Items[3].Image = Image.FromFile("Reject.ico");

cms.Items.Add("Un-Reject this Image");

cms.Items[4].Tag = "UnReject";

When the form is closed, the control is still in memory (I am using .NET
Memory Profiler 3.5). I have tried removing all items (cms.Items.Clear())
in the formclosing event, but it didn't help. Is there something else I
need to do to get this GCd?

Thanks,

Tim
 
P

Peter Duniho

Tim said:
I am using a context menu strip in my program (mdi). I populate the options
with the following code:

[...]
When the form is closed, the control is still in memory (I am using .NET
Memory Profiler 3.5). I have tried removing all items (cms.Items.Clear())
in the formclosing event, but it didn't help. Is there something else I
need to do to get this GCd?

I have a very vague recollection of a discussion in the
microsoft.public.dotnet.framework newsgroup in which someone else had a
similar problem. I don't remember the specifics, but I think it had
something to do with things in menus, and .NET somehow retaining a
reference to the things for some reason.

Whether it has anything to do with your specific situation, I don't
know. But it's probably worth your while to use Google Groups to search
for the discussion and see what came up (I don't even recall a
particular resolution to the issue being posted by the person).

Beyond that, if you expect more specific advice, you should create a
concise-but-complete code example that reliably reproduces the problem.
In this case, you should also describe exactly the steps needed in the
program (if any) to cause the issue to happen, as well as what
specifically you are seeing in the memory profiler.

On that latter point, I haven't used that memory profiler, but I would
expect that if it identifies an object still unfreed, it should also
identify what other objects have references to that object. So I'd
think that's the next place you should look. Specifically, if your
object isn't being freed, presumably some other object or variable still
has a reference to it. Find out what that is, and try to understand why
that's happening.

Pete
 
T

Tim Kelley

Here is a better example of what is going on:

Using VS2005

I create a new form (form1) and set the IsMdiContainer to 1

Create another form (form2) and add the contextMenuStrip to the form (using
the designer)
(I don't add any Items)

Create a button on form1 with the following code:
Form2 f2 = new Form2();
f2.MdiParent = this;
f2.Show();

Start the project, click the button to open form2, close form2 and take a
snapshot of
memory.

Thanks,

Tim
 

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