[英]GDI objects memory leak: Custom ToolStripControlHost within ContextMenuStrip is not disposing
我有各种基于ToolStripControlHost的自定义上下文菜单。 它们包装在ContextMenuStrip中,并放置在DataGridView列的标头中(取决于某些条件),如下所示:
if (this.DGV.Columns[DGVColname] != null)
{
ContextMenuStrip cstr = null;
// there is always only one item in context menu
// I tried disposing in different manners, this is an example of my efforts
if (this.DGV.Columns[DGVColname].HeaderCell.ContextMenuStrip != null)
{
cstr = this.DGV.Columns[DGVColname].HeaderCell.ContextMenuStrip;
if (cstr.Items[0] != null)
{
cstr.Items[0].Dispose();
cstr.Items.Clear();
}
}
else
{
cstr = new ContextMenuStrip();
cstr.Opened += new EventHandler(cstr_Opened);
}
TextBoxToolStrip tsHost = new TextBoxToolStrip();
tsHost.Size = new System.Drawing.Size(172, 20);
tsHost.TextChanged += new EventHandler(myToolStrip_TextChanged);
cstr.ShowCheckMargin = false;
cstr.ShowImageMargin = false;
cstr.Margin = new Padding(0);
cstr.Padding = new Padding(0);
cstr.Items.Add(tsHost);
cstr.MaximumSize = new Size(tsHost.Width + 10, tsHost.Height + 10);
cstr.Size = cstr.MaximumSize;
cstr.LayoutStyle = ToolStripLayoutStyle.Flow;
DGV.Columns[DGVColname].HeaderCell.ContextMenuStrip = cstr;
}
尽管调用了dispose和/或进行了任何设置,但我可以使我的应用程序处理的GDI对象数仍然为零,并且仍在稳定增长。 我的DataGridView中有20个带有菜单的列,每次调用该代码时,都会得到+30或+34(准确)。
在此示例中,TextBoxToolStrip扩展了ToolStripControlHost并包含单个TextBox:
public class TextBoxToolStrip : ToolStripControlHost
{
// .... some string or bool Properties here ....
public TextBox TextBoxControl
{
get { return Control as TextBox; }
}
public TextBoxToolStrip()
: base(new TextBox())
{
this.TextBoxControl.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.TextBoxControl.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.TextBoxControl.Location = new System.Drawing.Point(0, 3);
this.TextBoxControl.ReadOnly = false;
this.TextBoxControl.Font =
new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
this.TextBoxControl.Size = new System.Drawing.Size(172, 20);
this.Size = new System.Drawing.Size(172, 20);
this.TextBoxControl.TabIndex = 0;
this.TextBoxControl.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;
this.MouseHover += new EventHandler(TextBoxToolStrip_Enter);
this.AutoSize = false;
this.TextBoxControl.PreviewKeyDown += new PreviewKeyDownEventHandler(TextBoxPreviewKeyDown);
this.TextBoxControl.KeyDown += new KeyEventHandler(TextBoxControl_KeyDown);
}
protected override void OnSubscribeControlEvents(Control control)
{
base.OnSubscribeControlEvents(control);
TextBox tb = control as TextBox;
if (tb != null)
{
tb.TextChanged += new EventHandler(OnTextChanged);
}
}
protected override void OnUnsubscribeControlEvents(Control control)
{
base.OnUnsubscribeControlEvents(control);
TextBox tb = control as TextBox;
if (tb != null)
{
tb.TextChanged -= OnTextChanged;
}
}
private void TextBoxToolStrip_Enter(object sender, EventArgs e)
{
this.Focus();
}
private void TextBoxPreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Menu)
e.IsInputKey = true;
}
private void TextBoxControl_KeyDown(object sender, KeyEventArgs e)
{
TextBox txb = sender as TextBox;
ToolStripDropDown tsd = (ToolStripDropDown)txb.Parent;
if (e.KeyCode == Keys.Enter)
{
tsd.Close();
}
}
/// <summary>
/// Expose TextChanged event
/// </summary>
public new event EventHandler TextChanged;
private void OnTextChanged(object sender, EventArgs e)
{
if (TextChanged != null)
{
TextChanged(this, e);
}
}
题:
如何正确处理上下文菜单?
事实证明,它不是事件,静态引用或任何常见的东西。 我在TreeView上看到了ToolStripControlHosts,发现只有那些在泄漏。 看来TreeView会很高兴地自行泄漏资源:当CheckBoxes属性设置为true时,用于处理选中和未选中复选框的位图的句柄在处理TreeView时不会正确释放(每次在我的视图中都剩下4个)情况),这会导致GDI对象内存泄漏。 我不得不手动处理复选框图像列表。
这里描述了问题和解决方案。 尽管我在这里使用属性而不是事件:
public new bool CheckBoxes
{
get
{
return base.CheckBoxes;
}
set
{
if (base.CheckBoxes == false)
{
base.CheckBoxes = true;
IntPtr handle = SendMessage(this.Handle, TVM_GETIMAGELIST, new
IntPtr(TVSIL_STATE), IntPtr.Zero);
if (handle != IntPtr.Zero)
_checkboxImageList = handle;
}
}
}
在我的情况下,StyleChanged事件不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.