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