macos 如何让我的基本 SWT 应用程序在 Mac OS X 10.5.6 中正确退出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/483173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:01:33  来源:igfitidea点击:

How can I get my basic SWT application to exit properly in Mac OS X 10.5.6?

javamacosswt

提问by Alex Reynolds

I have the following SWT test code:

我有以下 SWT 测试代码:

public static void main(String[] args) {
    shell = new Shell();
    shell.setText(APP_NAME + " " + APP_VERSION);
    shell.addShellListener(new ShellListener() {
        public void shellActivated(ShellEvent event) { }
        public void shellClosed(ShellEvent event) { exit(); }
        public void shellDeactivated(ShellEvent event) { }
        public void shellDeiconified(ShellEvent event) { }
        public void shellIconified(ShellEvent event) { }
    });     
    shell.open();
    display = shell.getDisplay();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

My exit() method is as follows:

我的 exit() 方法如下:

private void exit() {
    System.exit(0);
}

I try to quit the application by closing the shell ("window") or by pulling down the application menu (labeled "SWT") and selecting "Quit".

我尝试通过关闭外壳(“窗口”)或下拉应用程序菜单(标有“SWT”)并选择“退出”来退出应用程序。

When I do this, a SWT stub is left behind in the Dock and the SWT application has not actually exited. I have to manually terminate the SWT application through Eclipse or via Force Quit.

当我这样做时,Dock 中会留下一个 SWT 存根,而 SWT 应用程序实际上并未退出。我必须通过 Eclipse 或强制退出手动终止 SWT 应用程序。

I have tried this with the v3.4 and v3.5 SWT jars, under Eclipse 3.4.1 under Mac OS X 10.5.6 (Intel).

我已经在 Mac OS X 10.5.6 (Intel) 下的 Eclipse 3.4.1 下使用 v3.4 和 v3.5 SWT jar 进行了尝试。

Is there additional work I need to do to be able to quit the application when I close the shell?

是否需要做额外的工作才能在关闭 shell 时退出应用程序?

采纳答案by McDowell

You are not releasing the native resources correctly - you have a resource leak.

您没有正确释放本机资源 - 您存在资源泄漏。

You don't need to do this:

你不需要这样做:

private void exit() {
    System.exit(0);
}

The main method will exit when the shell is disposed. If you mustuse an exit method, call it after you've disposed all SWT resources:

当 shell 被释放时,main 方法将退出。如果您必须使用退出方法,请在处理完所有 SWT 资源后调用它:

    Display display = new Display();
    try {
        Shell shell = new Shell(display);
        try {
            shell.open();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
        } finally {
            if (!shell.isDisposed()) {
                shell.dispose();
            }
        }
    } finally {
        display.dispose();
    }
    System.exit(0);

回答by Eddie

When you allocated the Shell:

当您分配 Shell 时:

shell = new Shell();

shell = new Shell();

some native resources were allocated along with it. You have to dispose of these resources before you exit your application:

一些本地资源也随之分配。在退出应用程序之前,您必须处理这些资源:

private void exit() {
    shell.dispose();
    System.exit(0);
}

Of course, you have to provide the "shell" variable to your exit() method to do this.

当然,您必须向 exit() 方法提供“shell”变量才能执行此操作。

Note that I don't believe that you need to dispose the Display, since you didn't create it with "new Display()". But anything in SWT (except for a few items where this is documented in the JavaDoc) that you create with newyou must dispose when you are finished with it. Otherwise you will leak native resources.

请注意,我认为您不需要处理 Display,因为您没有使用“ new Display()”创建它。但是,您在 SWT 中创建的任何内容(JavaDoc 中记录的一些项目除外)new都必须在完成后处理。否则,您将泄漏本机资源。