Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

macos

swt

I have the following SWT test code:

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:

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".

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.

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).

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

like image 324
Alex Reynolds Avatar asked Jan 27 '09 12:01

Alex Reynolds


2 Answers

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 must use an exit method, call it after you've disposed all SWT resources:

    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);
like image 192
McDowell Avatar answered Sep 28 '22 06:09

McDowell


When you allocated the 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.

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 new you must dispose when you are finished with it. Otherwise you will leak native resources.

like image 41
Eddie Avatar answered Sep 28 '22 06:09

Eddie