4
votes

I have the following class:

public class NewListBox : ListBox
    {
        public NewListBox()
        {
        }

        private ImageList _myImageList;

        public ImageList ImageList
        {
            get { return _myImageList; }
            set { _myImageList = value; }
        }
     }

I am interested in whether disposing of this object will trigger the disposal of fields on the object, such as the ImageList, or should i implement (override) the Dispose method and do this work myself?

4

4 Answers

6
votes

You should add the ImageList to your control's Components collection, then the base-class implementation of Dispose will Dispose everything in that collection, and you won't have to override Dispose yourself.

If you have any members that are IDisposable but are not Components, then you will have to override Dispose in your control and Dispose them yourself.

(I am using the term Component in the strict sense of objects that derive from System.ComponentModel.Component).

5
votes

this article is very helpful, in the Memory Disposal section.

All classes that implement IDisposable (including all Windows Forms controls) have a Dispose method. This method must be called when an object is no longer needed in order to release resources other than memory. There are two ways this happens:

  • manually (by calling Dispose explicitly)
  • automatically: by adding the object to a .NET container, such as a Form, Panel, TabPage or UserControl. The container will ensure that when it’s disposed, so are all of its members. Of course, the container itself must be disposed (or in turn, be part of another container). In the case of Windows Forms controls, we nearly always add them to a container – and hence rely on automatic disposal.
1
votes

Lot of different answers here ..

I strongly advise to read Garbage Collector Basics and Performance Hints In you case you've two option:

  • Dispose the ImageList manually, so the ressource will be released quickly (but not immediately)
  • Do nothing: the resources will be release next time the Garbage Collector analyse the Generation where you form is. If you form is closed, and nothing keep a reference to your form, then your form will be disposed, and then as no reference will point to the ImageList anymore, the ImageList will be disposed. The resources will be released, but a bit later than the first case.

Except if you have thousand of big images in your ImageList (or if you create/close the form hundred of times), you'll not notice any difference between the 2 cases

1
votes

Based on the code you have posted, you are not using Designer to implement this control. Thus, you will not have a designer-provided Dispose(bool disposing) method or a System.CompononetModel.IContainer components member that your extra control may be added to. I am not sure how ListBox handles its Controls property, but if it lets you register your ImageList instance there with Controls.Add(ImageList), that should get you the automatic Dispose() behavior.

Your other option is to override Control.Dispose(bool) like the following:

protected override void Dispose(bool disposing)
{
    // Only call Dispose() on members if invoked through a direct
    // call to `Dispose()`. (If disposing is false, that means
    // we are invoked through the finalizer and we should *only*
    // free up unmanaged resources that we *directly* own).
    if (disposing)
    {
        ImageList.Dispose();
    }

    base.Dispose(disposing);
}