Clearing controls from FlowLayoutPanel not calling destructors?
Asked Answered
D

4

4

Sorry if I'm missing something obvious, but I'm trying to clear the controls (a series of user controls) from a FlowLayoutPanel - (panelName).Controls.Clear();. Unfortunately this doesn't seem to be calling the destructors for the objects on the panel - the User Objects column in the task manager just keeps going up and up, until it hits 10,000 and throws an excecption.

Does anyone know what I'm missing here?

Dobbs answered 4/9, 2009 at 11:24 Comment(3)
Destructor is invoked by the GC.Cussed
Thanks - unfortunately no evidence of this ever happening.Dobbs
I was having the same problem with clearing a bunch of ZedGraph controls from a flowlayoutpanel. I eventually got a "error creating window handle" since it hit 10,000 USER Handles as well.Hamelin
D
2

Not a solution, but a workaround - the objects do seem to be destroyed by this (rough, from memory) code:

while(FlowLayoutPanel.Controls.Count > 0)
     FlowLayoutPanel.Controls.Remove(0);
Dobbs answered 9/9, 2009 at 10:39 Comment(2)
A slight variation is: while(FlowLayoutPanel.Controls.Count > 0) { FlowLayoutPanel.Controls.Clear(); }Glossology
The workaround still kept the user handles count growing for me (disposing Zedgraph control), however, if Ijust manually disposes the control after removing it from the flowlayoutpanel, that fixed it 100% for me (kept the user handles count constant)Hamelin
H
2

eftpotrm's workaround above still kept the user handles count growing for me, however, if you just manually dispose after removing the control, that fixed it 100% for me.

while (myFlowLayoutPanel.Controls.Count > 0)
{
     var controltoremove = myFlowLayoutPanel.Controls[0];
     myFlowLayoutPanel.Controls.Remove(controltoremove);
     controltoremove.Dispose();
}
Hamelin answered 1/8, 2012 at 15:59 Comment(0)
J
1

.NET does not have the concept of destructors. .NET has something called "finalizers" which look syntactically like destructors in C#. For more information, check out Jeff Richter's awesome book on how the CLR works -- CLR via C#.

You may want the objects to implement the IDisposable pattern, and then call their Dispose() method when you're done with them.

Jdavie answered 4/9, 2009 at 11:30 Comment(2)
Sorry, brain not in gear on the language, I meant finalizers I promise :-) Either way, it's not getting called. The custom control implements IDisposable, which does all it's supposed to. I've tried with a finalizer in case, also destroying the children. I've tried explicitly calling the garbage collector - in all cases, user objects just marches up to 10k and throws an exception.Dobbs
What's the exception? From what you say, I don't think you fully understand the mechanisms of finalizers and the IDisposable interface...Jdavie
S
0

Try using a memory profiler, (e.g. ants) it will tell you what is keeping the control alive. Trying to 2nd guess this type of problem is very hard.

Red-gate gives 14 days tail that should be more then enough time to tack down this problem and decide if a memory profiler provides you with long term value.

There are lots of other memory profilers on the market (e.g. .NET Memory Profiler) most of them have free trials, however I have found that the Red-Gate tools are easy to use, so tend try them first.

Shearin answered 24/9, 2009 at 17:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.