C
C#4mo ago
Amir

✅ Winforms not freeing memory when disposing of control

void NewButtonOnClick(Button btn)
{
//Delete the button completely, write code
btn.Parent.Controls.Remove(btn);
btn.Dispose();
btn = null;
}
void NewButtonOnClick(Button btn)
{
//Delete the button completely, write code
btn.Parent.Controls.Remove(btn);
btn.Dispose();
btn = null;
}
The memory usage continues going up
5 Replies
Fremko
Fremko4mo ago
How do you initialise the button?
Amir
AmirOP4mo ago
void OnClick(object sender, EventArgs e)
{
Button newButton = new()
{
Text = "New Button",
BackColor = Color.White,
ForeColor = Color.Black,
Size = new Size(100, 50),
TextAlign = ContentAlignment.MiddleCenter
};

newButton.Click += (s, e) => NewButtonOnClick(newButton);

flowLayoutPanel.Controls.Add(newButton);
newButton.Show();
}
void OnClick(object sender, EventArgs e)
{
Button newButton = new()
{
Text = "New Button",
BackColor = Color.White,
ForeColor = Color.Black,
Size = new Size(100, 50),
TextAlign = ContentAlignment.MiddleCenter
};

newButton.Click += (s, e) => NewButtonOnClick(newButton);

flowLayoutPanel.Controls.Add(newButton);
newButton.Show();
}
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Amir
AmirOP4mo ago
So I'm just supposed to leave it up to the GC and not worry about the memory management
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?