Home Forums WinForms controls Other WinForms controls memory leak in tabbedmdimanager

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #18486 |

    for your example, TabbedMdiImageViewer

    add GC.Collect(); in closeAllToolStripMenuItem_Click

    and add  

    ~PictureForm()
        {
        }

     set breakpoint in ~PictureForm()

     

    run, you’ll find that pictureform never free.

    Imported from legacy forums. Posted by progame (had 2564 views)

    User (Old forums)
    Member
    Post count: 23064

    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#.

     Morgan

    Imported from legacy forums. Posted by Morgan (had 3643 views)

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.