Thanks for the report. This seems to be a .NET Framework bug. I've been working the past several hours on it. What is happening is that something in the framework is retaining a reference to an Icon on the menu popup forms. So each time you open a menu, a popup form is created behind the scenes, and something in Microsoft's code is retaining the Form.Icon reference in memory.
We have even been able to duplicate this bug with a regular Form like with this code:
Form f = new Form();
f.Show();
f.Close();
f.Dispose();
If you run that and check the user handles with a memory profiler, you'll see that an HICON is still referenced!
Oddly enough if you change the code to this, it doesn't retain any HICONs:
Form f = new Form();
f.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
f.Show();
f.Close();
f.Dispose();
So it seems like unless you use a FixedDialog, any Form you create in Windows Forms has a memory leak. I have been scouring the Internet for reports of this but am not finding much of anything.
Since our popup forms for the menus are being created often, that's why you see the resources going up.
We also located a second place that is a similar issue. When drawing the child MDI form's icon on the main menu bar, we are leaking there too. And again, it's not our code but is something internal to the .NET Framework.
Our code for that part is here:
// Need to do this to get the correct icon color depth
Icon icon = new Icon(form.Icon, bounds.Width, bounds.Height);
e.Graphics.DrawIcon(icon, bounds);
icon.Dispose();
We have to create a new Icon from the Form.Icon otherwise, we get the 32x32 size icon and not the 16x16 one and this is the only way we've found to get that size part of icon. As you can see we are properly disposing the icon but when you profile it, something in the framework is still retaining a reference to it.
Has anyone else run across these issues? We'll continue to try and come up with a way around them. Perhaps for the first one, we will have to change our code to retain the popup forms in memory instead of getting rid of them after the menus are closed. For the second one, we need to find a way to get the 16x16 size part of an icon so if anyone knows of another way to do it, please post how.