C
C#2mo 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
Fremko2mo ago
How do you initialise the button?
Amir
AmirOP2mo 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 User2mo ago
Message Not Public
Sign In & Join Server To View
Amir
AmirOP2mo ago
So I'm just supposed to leave it up to the GC and not worry about the memory management
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server