Problems with freeing memory
I have a form with the following fields:
List<String^> ^images;
PictureBox ^box;.
List<Picture开发者_StackOverflowBox^> ^boxes;
String ^path;
(as well as some ints).
I allocate the memory for each of these with gcnew
, but when I close the form, the memory is not freed. I thought they would be garbage collected; why are they not?
I also noticed an autogenerated components
variable of type Container ^
that's delete
d in the destructor. What's up with that?
EDIT: Whoops, forgot one rather important thing: In the main form from which I call the above forms, I have a List of these forms (to communicate with them). How should I remove the form from the list once it's closed?
Garbage collection is indeterminate; you can't predict when it will run. All that's guaranteed is that it will run "when it needs to"; if you absolutely need to control when it happens, then you can call:
GC.Collect();
and
GC.WaitForPendingFinalizers();
精彩评论