Bryan Brannon

I have a problem with a possible memory leak in my WinForms app. I finally was able to figure this out and reproduce by doing the floowing.

Add a FlowLayoutPanel to a form and have a process that adds (multiple) Buttons at runtime. Then i have a method that is called from an event (button click) that removes the controls like this:

Code Snippet

then call the method to re-add them back using:

Code Snippet

Me.FlowLayoutPanel1.Controls.Add(New Button...)

This works except for the Mem Usage on the machine. This goes thru the roof after loading 30 controls over and over again.

I have tried all of the following without luck:

Code Snippet
For Each ctrl As Control In Me.FlowLayoutPanel1.Controls
ctrl = Nothing

Any ideas

Thanks in advanced.

Re: Windows Forms General Memory Leak using FlowLayoutPanel?


I've can partly reproduce your problem. This is the test code I used:

private void button1_Click(object sender, EventArgs e) {
for (int ix = 0; ix < 10000; ++ix) {
flowLayoutPanel1.Controls.Add(new Button());
//foreach (Control ctl in flowLayoutPanel1.Controls) ctl.Dispose();

This code works fine, no memory increase visible. If I comment the DoEvents() call, memory usage increases until the program bombs because it runs out of Windows handles. If I call ctl.Dispose() explicitly it works fine again.

Nothing in Product Feedback either. Nothing special in FlowLayoutPanel that would explain what you see, it inherits from Panel. Not sure if this was helpful...