A couple of things to consider. First, GC.Collect is not a deterministic command. The .Net garbage collector is free to ignore the request and not do garbage collection when the code requests it. Second, the closeAllToolStripMenuItem_Click method calls Form.Close on each of the forms. According to the documentation for Form.Close…
When a form is closed, all resources created within the object are closed and the form is disposed.
The forms are disposed and placed on the heap for garbage collection. The destructor is not necessarily called right then. This the major difference between C# destructors and C++ destructors. In C++, the destructor is deterministic to the developer. That is, when you dispose of the form, the destructor is called. In C#, it is not deterministic. The destructor is called by the GC when it’s good and ready to call it, according to its own criteria. When you dispose of a form (by calling Close, for example), it may or may not call the destructor. It will, of course, call the destructor eventually, when the application exits if nothing else.
Third, even if the destructor is never called, this does not mean there’s a memory leak in the TabbedMDIManager. All the code in the method you indicated is doing is iterating through a collection of forms and calling the Close method on each one. As the last form in a TabGroup is called, the TabGroup is removed from the collection (or so it appears to me, I may be wrong).
There are some very good articles on .Net garbage collection and memory management in general out there. I recommend reading some of them, especially if you come from a C/C++ background. It’s quite different in C#.
Imported from legacy forums. Posted by Morgan (had 3643 views)