WinForms and Recycling Custom Controls

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 the disposal of this object will initiate the removal of fields on the object, such as ImageList, or should I implement (override) the Dispose method and do this work myself?

+4
source share
4 answers

You must add ImageList to your collection of Control components, then the implementation of the Dispose class will destroy everything in this collection, and you will not have to redefine Dispose yourself.

If you have any elements that are IDisposable but not components, then you will have to override Dispose in your control and dispose of it yourself.

(I use the term Component in the strict sense of objects that come from System.ComponentModel.Component).

+5
source

This article is very useful in the "Memory Utilization" section.

All classes that implement IDisposable (including all Windows Forms controls) have a Dispose method. This method should be called when the object is no longer needed to free resources other than memory. This happens in two ways:

  • manually (calling Dispose explicitly)
  • automatically: adding an object to a .NET container, such as a form, panel, tab, or UserControl. The container guarantees that when it is installed, all its members. Of course, the container itself must be located (or, in turn, be part of another container). In the case of Windows Forms controls, we almost always add them to the container - and therefore rely on automatic deletion.
+5
source

There are many different answers.

I highly recommend reading the Garbage Collector Basics and Performance Recommendations. In this case, you have two options:

  • Dispose of the ImageList manually so that the ressource will be released quickly (but not immediately)
  • Do nothing: resources will be released next time the Garbage Collector analyzes the generation in which you form. If you form is closed, and nothing contains a link to your form, then your form will be deleted, and then, since no link points to ImageList, ImageList will be deleted. Resources will be released, but a little later than the first.

If you have thousands of large images in your ImageList (or if you create / close a form hundreds of times), you will not notice the difference between the two cases

+1
source

Based on the code you posted, you are not using Designer to implement this control. Thus, you will not have the Dispose(bool disposing) method provided by the designer or the System.CompononetModel.IContainer components member to which you can add an additional control. I'm not sure how the ListBox handles its Controls property, but if it allows you to register your instance of ImageList there with Controls.Add(ImageList) , this should lead to automatic Dispose() behavior.

Another option is to override Control.Dispose(bool) as follows:

 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); } 
0
source

Source: https://habr.com/ru/post/1412962/


All Articles