简体   繁体   English

我是否需要明确处理SWT Shell?

[英]Do I need to explicitly dispose SWT Shells?

I have been told and have read that SWT Objects must be explicitly disposed by calling their dispose method. 我被告知并且已经读过 SWT对象必须通过调用它们的dispose方法来明确dispose However, in my own testing with the following code, I have noted that at least Shell s report themselves as disposed even though the dispose method is never called (nor appears) anywhere in my code. 但是,在我自己使用以下代码进行的测试中,我注意到至少Shell报告自己已经处理掉了,即使我的代码中的任何地方都没有调用dispose方法(也没有出现)。

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? 如果是这样,你怎么知道何时处置Shell,以及dispose方法应该出现在哪里?

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. 当用户关闭窗口时,将释放shell及其子项。

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. 你也不需要调用dispose在任何儿童,作为处置父母会为你做的。 Again, from the link you cite: 再次,从您引用的链接:

When you dispose a Shell, its children are disposed. 当您处置Shell时,其子项将被处置。 In fact, disposing any Composite will dispose all of the Composite's children. 实际上,处置任何Composite都会处理所有Composite的子节点。

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. 你明确需要调用他们的dispose方法。 It's best to hook a dispose listener to the Composite you're using them in to do this. 最好将一个dispose监听器挂钩到你正在使用它们的Composite上来执行此操作。 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 Color s that you use but do not create . 但是,重要的是要注意,您不应该使用使用但不创建的 Color For example, do not dispose the system colors available from Display#getSystemColor() . 例如,不要处理Display#getSystemColor()可用的系统颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM