Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to explicitly dispose SWT Shells?

Tags:

java

swt

I have been told and have read that SWT Objects must be explicitly disposed by calling their dispose method. However, in my own testing with the following code, I have noted that at least Shells report themselves as disposed even though the dispose method is never called (nor appears) anywhere in my code.

import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;


    public class Test {

        private static int numDisposals = 0;
        private static List<Shell> shells = new ArrayList<Shell>();

    public static void main(String[] args) {
        Display d = Display.getDefault();
        for (int i = 0; i < 3; i++) {
            Shell s = new Shell(d);
            shells.add(s);
            s.setText(String.valueOf(i));
            s.open();
            s.addDisposeListener(new DisposeListener() {

                @Override
                public void widgetDisposed(DisposeEvent notUsed) {
                    numDisposals++;
                    printShellStatus();
                }
            });
        }
        while (numDisposals < 3) {
            while (!d.readAndDispatch()) {
                d.sleep();
            }
        }
        printShellStatus();
    }

    public static void printShellStatus() {
        System.out.println("Which shells are disposed?");
        for (Shell shell : shells) {
            if (shell.isDisposed()) {
                System.out.println("I am disposed.");
            } else if (!shell.isDisposed()) {
                System.out.println("I am NOT disposed.");
            }
        }
    }
}

So does Shell really need to be explicitly disposed? If so, how do you know when to dispose a Shell, and where should the dispose method appear?

like image 739
Buttons840 Avatar asked Jan 12 '12 21:01

Buttons840


1 Answers

The paper that you cite makes this clear:

Widgets themselves do not usually need to be disposed programmatically. A shell and its children are disposed when the user closes its window.

So while a shell does need to be disposed, the burden is not on you to do so. Nor do you need to call dispose on any of the children, as disposing a parent will do that for you. Again, from the link you cite:

When you dispose a Shell, its children are disposed. In fact, disposing any Composite will dispose all of the Composite's children.

However, you do have to make sure that you dispose resources you create that are not children. For example: colors and fonts. You do explicitly need to call their dispose method. It's best to hook a dispose listener to the Composite you're using them in to do this. For example:

public class MyComposite extends Composite
{
    private final Color color;

    public MyComposite(Composite parent, int style)
    {
        super(parent, style);

        color = new Color(getShell().getDisplay(), 255, 255, 255);

        addDisposeListener(new DisposeListener() {
            public void widgetDisposed(DisposeEvent e)
            {
                color.dispose();
            }
        });
    }
}

It's important to note, however, that you should not dispose Colors that you use but do not create. For example, do not dispose the system colors available from Display#getSystemColor().

like image 152
Edward Thomson Avatar answered Oct 19 '22 01:10

Edward Thomson