[Eclipse notes] Effective management of graphics system resources in SWT

Source: Internet
Author: User
Tags constructor inheritance reference resource thread
Notes | graphics
Part of this article and inspiration from the eclipse.org website is hereby declared. For more information, please refer to:

Http://eclipse.org/articles/Article-SWT-Design-2/SWT-Design-2.html



Because SWT deals directly with the operating system, we need to be careful with the graphics resources of the system to avoid unnecessary resource leaks. Fortunately SWT provides a good resource management mechanism, and in most cases what we need to do is to ensure two principles:

The first principle – who assigns who to destroy

Second rule – destroying child controls while the parent control is destroyed



Let's take a look at how these two are embodied in practice.

First of all, look at principle one. At first glance this seems to be nonsense, but in practice it is often not so simple. First, the construction method is not equal to allocating resources, and actually allocating resources can occur anywhere in a class and at any time of an object's lifecycle, as long as your code tells the operating system to do so. You must ensure that all resources allocated by you call their Dispose () method when you are no longer in use, and you must also make sure that all resources not allocated by you do not casually call their Dispose () method, or it will likely affect the actual allocation of the relevant code to work properly. The good news is that, in order to clarify and simplify the division of labour as set out in this first principle, SWT was determined at the beginning of the design that all SWT classes based on system resources complete all the required resource allocations in their construction methods, and in other ways there is no action to allocate system resources. So we can be lucky to look at SWT's resource management: If you call the construction method of an SWT class, then you call its Dispose () method to release the resource, and if you do not invoke the construction method of an SWT class, even if you use an instance of the class, Nor should you call its Dispose () method. It's so clear.

For example, if you're new to a Font object, you should call Dispose () when you no longer need it, and if you get a Font object through a control's GetFont () method and use it, you should not destroy it, but rather give it to that particular control.

For the second principle, SWT has a good mechanism to support it, and that is, all SWT controls, specifically, instances of the composite class and its subclasses, must have a parent control whose reference is passed in to the child control's construction method. It is to be noted that all of these controls have a default access level without parameters, so we cannot invoke such a default constructor directly in our SWT program and only provide a reference to a parent control, whereas in the Composit constructor method of the Widget class (the parent Class), It calls the following methods:


void Checkparent (widget parent) {if (parent = null) error (SWT.       Error_null_argument); Parent.checkwidget ();}
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);}
Such checks ensure that any control has a parent control when it is created. When we call a composite Dispose () method, it calls:


void Releasechildren () {control [] children = _getchildren ();              for (int i=0; i<children.length; i++) {Control Child = children [I];       if (!child.isdisposed ()) child.releaseresources (); }}
The _getchildren () method iterates through the control's child controls through the OS object's methods, and then totals them together to call the Releaseresources () method to release the control and handle.

Back to the last example of 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 (display);         shell.pack ();         shell.open ();        while  (! Shell.isdisposed ())  {            if  (! Display.readanddispatch ())  {                 display.sleep ();             }        }         display.dispose ();     }}


In this, display is a top-level device that inherits from the device class, and the device class implements the Drawable interface. The shell's parent class is decoration, and decoration inherits from the Canvas,canvas inheritance from composite, and ultimately this inheritance chain is connected to the Widget class. When we create the shell example, we need to tell the constructor what its parent control is, and here is display. So when we finally called Display.dispose (), even though we didn't explicitly write Shell.dispose (), our shell instance was destroyed.

This is SWT's resource management mechanism, with the exception of the MenuItem SetMenu () method and the SetMenu () method of control, which registers its own parent controls by explicitly calling SetMenu.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.