How deep does Controls.Clear() clean up?
Clear doesn't dispose the controls, leading to a memory leak. From the link:
Calling the Clear method does not remove control handles from memory. You must explicitly call the Dispose method to avoid memory leaks.
Since disposing within a loop messes up the indexing, you can either copy the control collection to another list and perform a ForEach
loop on them or use a backwards For
loop.
for (int i = myTableLayoutPanelControls.Count - 1; i >= 0; --i)
myTableLayoutPanelControls[i].Dispose();
Calling Dispose
will remove the controls from memory (when the GC picks it up). This will also handle the calling of the child control's Dispose
method.
One catch is if you've got a custom control that implements IDisposable
or you're overriding the Dispose
method without calling the base
method. In your object's Dispose
method you need to ensure that you've unsubscribed from any events outside your scope. If you don't, that reference will keep your object alive.