المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Weird, unexplained occurance...



C# Programming
09-03-2009, 11:01 AM
I've been working on a CRS system, and was running into a nasty error...

For some reason every ~16 or so search queries, the program would crash, and give me a WIN32 exception, telling me that my program had used up all its allocated handles.

Ok, so I wasn't disposing of my objects correctly somewhere...

After much searching, I found that the foreach loop that was disposing of the results was not working as expected. (the search results were displayed using Panels).

If I called:


int i = ********FlowPanel.Controls.Count;


It would return the expected number of (say) 20.

However when I ran:


foreach (Control c in ********FlowPanel.Controls)
{
c.Dispose();
}


It would cut out of the foreach loop at the 10th object.

If I ran the same foreach loop directly afterwards, it would cut out at the 5th object etc etc.

I solved the problem by using this snippet:


while (********FlowPanel.Controls.Count > 0)
{
foreach (Control c in ********FlowPanel.Controls)
{
c.Dispose();
}
}

GC.Collect();


This solved the problem, but can anyone explain this behaviour?

Cheers,


Mark.

Mark Brock
"We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen