Search code examples
.netwinformsgarbage-collectiondestructorflowlayoutpanel

Clearing controls from FlowLayoutPanel not calling destructors?


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?


Solution

  • 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);