Q
q23r
I saw many memory leaks problems in this group.
But It seems that it's the new one.
When you add controls to the form, then delete them using
this.Controls.Clear(), memory is not released.
Simple example:
private void button1_Click(object sender, EventArgs e)
{
for (int k = 0; k < 50; k++)
{
this.SuspendLayout();
for (int i = 0; i < 100; i++)
{
CreateButton();
CreateText();
this.Text = String.Format("k={0} i={1}", k, i);
}
this.ResumeLayout(true);
this.panel1.Controls.Clear();
}
}
We create 100 buttons and textboxes, then removes them by
this.panel1.Controls.Clear().
And repeat this for 50 times.
Actually on the 22-25 time we have an exception "Can't create windows
handle".
And memory grows during each cycle.
Is it known bug or not?
For those who interested in details:
private void CreateButton()
{
Button button = new Button();
button.Location = new System.Drawing.Point(6, _position += 25);
button.Name = "Test";
button.Size = new System.Drawing.Size(75, 23);
button.TabIndex = 0;
button.Text = "Test";
this.button1.Text = ((int)(_position / 25)).ToString();
button.UseVisualStyleBackColor = true;
this.panel1.Controls.Add(button);
}
private void CreateText()
{
TextBox text = new TextBox();
text.Location = new System.Drawing.Point(6, _position += 25);
text.Name = "Text";
text.Size = new System.Drawing.Size(75, 23);
text.Text = "Text";
this.panel1.Controls.Add(text);
}
But It seems that it's the new one.
When you add controls to the form, then delete them using
this.Controls.Clear(), memory is not released.
Simple example:
private void button1_Click(object sender, EventArgs e)
{
for (int k = 0; k < 50; k++)
{
this.SuspendLayout();
for (int i = 0; i < 100; i++)
{
CreateButton();
CreateText();
this.Text = String.Format("k={0} i={1}", k, i);
}
this.ResumeLayout(true);
this.panel1.Controls.Clear();
}
}
We create 100 buttons and textboxes, then removes them by
this.panel1.Controls.Clear().
And repeat this for 50 times.
Actually on the 22-25 time we have an exception "Can't create windows
handle".
And memory grows during each cycle.
Is it known bug or not?
For those who interested in details:
private void CreateButton()
{
Button button = new Button();
button.Location = new System.Drawing.Point(6, _position += 25);
button.Name = "Test";
button.Size = new System.Drawing.Size(75, 23);
button.TabIndex = 0;
button.Text = "Test";
this.button1.Text = ((int)(_position / 25)).ToString();
button.UseVisualStyleBackColor = true;
this.panel1.Controls.Add(button);
}
private void CreateText()
{
TextBox text = new TextBox();
text.Location = new System.Drawing.Point(6, _position += 25);
text.Name = "Text";
text.Size = new System.Drawing.Size(75, 23);
text.Text = "Text";
this.panel1.Controls.Add(text);
}