Part of this article and inspiration from the Eclipse.org website, this statement. For more information, please refer to:
Http://eclipse.org/Articles/Article-Swt-Design-2/swt-design-2.html
Since SWT is dealing with the operating system, we need to be careful when processing the graphics resources of the system to avoid unnecessary resource leaks. Fortunately, SWT provides a good resource management mechanism, and we need to do it in most cases to ensure two principles:
The first principle - Who is distributed?
Article 2 Principles - Decline of destruction of the child while the parent control is destroyed
Let's take a look at how these two are reflected in the actual situation.
First see the first principle. At first glance, this seems to be nonsense, but it is often not that simple in practice. First, the constructor does not mean the allocation of resources, and actually allocate resources anywhere in a class and at any time of an object lifecycle, as long as your code tells the operating system. You must guarantee that all resources you assigned when you are no longer in use; you must also guarantee that all resources you assigned should not call its Dispose () method, otherwise it is likely to affect the actual The normal operation of the relevant code allocated. The good news is that in order to clearly and simplify the division of labor specified in this first principle, SWT is determined at the beginning of the design. All system resources-based SWT classes complete all the required resource allocations in their constructors, in other There is no action for allocating system resources, so we can watch SWT resource management: if you call a SWT structure constructor, then you call its Dispose () method to release resources; if You didn't call a SWT class constructor, even if you use this class's instance, you should not call its Dispose () method. It is so clear.
For example, you will call Dispose () if you no longer need it, if you get a font object and use it, you should not destroy it after you use a Font object through a control GetFont () method. It should be handed over to the specific control.
For the second principle, SWT has a good mechanism to support it, that is, all SWT controls, concrete, COMPOSITE classes, and instances of their subclasses must have a parent control, this parent control references The structure of the sub-control is incorporated. It should be noted that all of these controls have only the default access level, so we cannot call such a default constructor directly in their own SWT program and can only provide reference to a parent control, while in Widget In the band parameter constructor of the class (the parent class of Composit), it will call the following methods:
Void
Checkparent (Widget Parent)
{IF (parent == null) error (swt.error_null_argument); parent.checkwidget ();}
and then:
protected
Void
Checkwidget ()
{Display display = this.display; if (display == null) error (SWT.ERROR_WIDGET_DISPOSED); if (display.thread = Thread.currentThread ()!) Error (SWT.ERROR_THREAD_INVALID_ACCESS); if ((state & DISPOSED)! = 0) Error (swt.error_widget_disposed);} This check guarantees that any control has a parent control when creating. When we call a Composite's Dispose () method, it calls:
Void
ReleaseChildren ()
{Control [] Children = _GetChildren (); for (int i = 0; i The _Getchildren () method traverses the child control of the control through the OS object, and then collects the ReleaseResources () method to release the control and handle together. Back to the last Simplestswt: Package sean.test.swt; import org.eclipse.swt.widgets.display; import org.eclipse.swt.widgets.shell; public Class Simplestswt {Public static void main (string [] args) {Display Display = new display (); shell shell = new shell; shell.pack (); shell.Open (); while (! Shell.isdisposed ()) {If (! Display.readddispatch ()) {Display.sleep ();}} DISPLAY.DISPOSE ()}} Among this, Display is a top-level device that inherits from the Device class, while the DEVICE class implements the Drawable interface. The parent class of the shell is Decoration, while Decoration inherits from Canvas, Canvas inherits from Composite, and finally this inheritance chain has been connected to the Widget class. When we create a shell example, we need to tell the constructor what is the parent control, here is Display. So when we finally called Display.Dispose (), although we didn't clearly write shell.dispose (), our shell instance was destroyed. This is the resource management mechanism of the SWT, a slight exception is the MenuItem's setMenu () method and the Control's setMenu () method, which registers its own parent control by explicitly calling SetMenu.