Right method to exit/dispose on linux

classic Classic list List threaded Threaded
30 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Right method to exit/dispose on linux

elect
Hi,

playing with some tutorials on linux (Ubuntu 13.10 64b) I have problems in quitting my application

Here a small case

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ogldevtutorials.tutorial04;

import com.jogamp.newt.awt.NewtCanvasAWT;
import com.jogamp.newt.event.awt.AWTWindowAdapter;
import com.jogamp.newt.opengl.GLWindow;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;

/**
 *
 * @author elect
 */
public class Tutorial04 implements GLEventListener {

    public static void main(String[] args) {

        final Tutorial04 tutorial04 = new Tutorial04();

        final Frame frame = new Frame("Tutorial 04");

        frame.add(tutorial04.getNewtCanvasAWT());

        frame.setSize(tutorial04.getGlWindow().getWidth(), tutorial04.getGlWindow().getHeight());

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent windowEvent) {
                tutorial04.getGlWindow().destroy();
                frame.dispose();
                System.exit(0);
            }
        });
//        frame.addWindowListener(new WindowAdapter() {
//            public void windowDestroyNotify(WindowEvent arg0) {
//                tutorial04.getGlWindow().destroy();
//                frame.dispose();
//                System.exit(0);
//            }
//        });

        frame.setVisible(true);
    }

    private GLWindow glWindow;
    private NewtCanvasAWT newtCanvasAWT;
    private int imageWidth;
    private int imageHeight;

    public Tutorial04() {

        imageWidth = 1024;
        imageHeight = 768;

        initGL();
    }

    private void initGL() {
        GLProfile gLProfile = GLProfile.getDefault();

        GLCapabilities gLCapabilities = new GLCapabilities(gLProfile);

        glWindow = GLWindow.create(gLCapabilities);

        newtCanvasAWT = new NewtCanvasAWT(glWindow);

        glWindow.setSize(imageWidth, imageHeight);

        glWindow.addGLEventListener(this);
    }

    @Override
    public void init(GLAutoDrawable glad) {
        System.out.println("init");

    }

    @Override
    public void dispose(GLAutoDrawable glad) {
        System.out.println("dispose");
    }

    @Override
    public void display(GLAutoDrawable glad) {
        System.out.println("display");
    }

    @Override
    public void reshape(GLAutoDrawable glad, int i, int i1, int i2, int i3) {
        System.out.println("reshape (" + i + ", " + i1 + ") (" + i2 + ", " + i3 + ")");
    }

    public NewtCanvasAWT getNewtCanvasAWT() {
        return newtCanvasAWT;
    }

    public GLWindow getGlWindow() {
        return glWindow;
    }
}


If I don't add any WindowListener then I can't close the window off course...

If inside the WindowClosing() I have only the System.exit(0) I get

X11Util.Display: Shutdown (JVM shutdown: true, open (no close attempt): 3/3, reusable (open, marked uncloseable): 0, pending (open in creation order): 3)
X11Util: Open X11 Display Connections: 3
X11Util: Open[0]: NamedX11Display[:0, 0x7fa5e84f99b0, refCount 1, unCloseable false]
X11Util: Open[1]: NamedX11Display[:0, 0x7fa5a40c9a70, refCount 1, unCloseable false]
X11Util: Open[2]: NamedX11Display[:0, 0x7fa5a40dd630, refCount 1, unCloseable false]


If I add frame.dispose(); I get a VM crash

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007f9e25eb4736, pid=4400, tid=140317223712512
#
# JRE version: Java(TM) SE Runtime Environment (7.0_51-b13) (build 1.7.0_51-b13)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (24.51-b03 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C  [libGL.so.1+0x87736]  glXCreateNewContext+0x4956
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/elect/Documents/oglDevTutorials/oglDevTutorials/hs_err_pid4400.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.sun.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
Java Result: 134


If I add the glWindow.dispose() nothing changes, still VM crash


So, how am I supposed to do it?

On windows I just do

frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent windowEvent) {
                System.exit(0);
            }
        });


Ps: I do not use animator ^^, please don't be angry with me for that :D
Reply | Threaded
Open this post in threaded view
|

Re: Right method to exit/dispose on linux

gouessej
Administrator
Hi

Do you reproduce your bug when you don't use NewtCanvasAWT, with pure NEWT?
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Right method to exit/dispose on linux

elect
gouessej wrote
Hi

Do you reproduce your bug when you don't use NewtCanvasAWT, with pure NEWT?
Sorry but I don't get what you meant by "pure NEWT"
Reply | Threaded
Open this post in threaded view
|

Re: Right method to exit/dispose on linux

gouessej
Administrator
elect wrote
Sorry but I don't get what you meant by "pure NEWT"
No AWT, no Swing, no SWT, no Qt, ..., no bridge, only GLWindow and nothing else.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Right method to exit/dispose on linux

elect
gouessej wrote
No AWT, no Swing, no SWT, no Qt, ..., no bridge, only GLWindow and nothing else.
Everything ok, but I do not have anything, neither the window, I only instantiate my tutorial04...
Reply | Threaded
Open this post in threaded view
|

Re: Right method to exit/dispose on linux

gouessej
Administrator
Instead of using an AWT Frame, a NewtCanvasAWT and a GLWindow, I suggest you to use correctly a single GLWindow, set its size, call setVisible(true), etc... I try to know whether your bug comes from NEWT, from AWT or from the NEWT AWT "bridge".
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Right method to exit/dispose on linux

elect
gouessej wrote
Instead of using an AWT Frame, a NewtCanvasAWT and a GLWindow, I suggest you to use correctly a single GLWindow, set its size, call setVisible(true), etc... I try to know whether your bug comes from NEWT, from AWT or from the NEWT AWT "bridge".
Ah, I got it :)

So:

        final Tutorial04 tutorial04 = new Tutorial04();

        GLWindow window = tutorial04.getGlWindow();
       
        window.setSize(window.getWidth(), window.getHeight());
       
        window.setVisible(true);


Running fine for one second, then I get:

init
reshape (0, 0) (1024, 768)
display
display
display
X11Util.Display: Shutdown (JVM shutdown: true, open (no close attempt): 2/2, reusable (open, marked uncloseable): 0, pending (open in creation order): 2)
X11Util: Open X11 Display Connections: 2
X11Util: Open[0]: NamedX11Display[:0.0, 0x7ffc3c09dd70, refCount 1, unCloseable false]
X11Util: Open[1]: NamedX11Display[:0.0, 0x7ffc3c0d65a0, refCount 1, unCloseable false]


As you can see, display is called 3 times and then crash. In that 3 calls, it actually draws properly (a red triangle)
Reply | Threaded
Open this post in threaded view
|

Re: Right method to exit/dispose on linux

Xerxes RÃ¥nby
elect wrote
gouessej wrote
Instead of using an AWT Frame, a NewtCanvasAWT and a GLWindow, I suggest you to use correctly a single GLWindow, set its size, call setVisible(true), etc... I try to know whether your bug comes from NEWT, from AWT or from the NEWT AWT "bridge".
Ah, I got it :)

So:

        final Tutorial04 tutorial04 = new Tutorial04();

        GLWindow window = tutorial04.getGlWindow();
       
        window.setSize(window.getWidth(), window.getHeight());
       
        window.setVisible(true);


Running fine for one second, then I get:

init
reshape (0, 0) (1024, 768)
display
display
display
X11Util.Display: Shutdown (JVM shutdown: true, open (no close attempt): 2/2, reusable (open, marked uncloseable): 0, pending (open in creation order): 2)
X11Util: Open X11 Display Connections: 2
X11Util: Open[0]: NamedX11Display[:0.0, 0x7ffc3c09dd70, refCount 1, unCloseable false]
X11Util: Open[1]: NamedX11Display[:0.0, 0x7ffc3c0d65a0, refCount 1, unCloseable false]


As you can see, display is called 3 times and then crash. In that 3 calls, it actually draws properly (a red triangle)
This is expected, and is not a crash.
Your only thead, the main thread, has exited and you see the four X11Util lines when jogamp cleaning up before the jvm exit your application. You will see these lines unless your GLWindow has explicited been closed by you before the application exit.

If you want your application to keep running after main has exited then you should have spawned a new thread that keeps your application alive.
You can if you want keep the main thread alive in a loop that wait untill the glwindow has closed.
Reply | Threaded
Open this post in threaded view
|

Re: Right method to exit/dispose on linux

elect
Xerxes RÃ¥nby wrote
This is expected, and is not a crash.
Your only thead, the main thread, has exited and you see the four X11Util lines when jogamp cleaning up before the jvm exit your application. You will see these lines unless your GLWindow has explicited been closed by you before the application exit.

If you want your application to keep running after main has exited then you should have spawned a new thread that keeps your application alive.
You can if you want keep the main thread alive in a loop that wait untill the glwindow has closed.
Ok, but why then on windows it doesn't happens?
Reply | Threaded
Open this post in threaded view
|

Re: Right method to exit/dispose on linux

gouessej
Administrator
You just benefited of the timing but it may occur under Windows too as the main constraint is the same even though the log messages might be different.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Right method to exit/dispose on linux

elect
In reply to this post by Xerxes RÃ¥nby
Xerxes RÃ¥nby wrote
If you want your application to keep running after main has exited then you should have spawned a new thread that keeps your application alive.
You can if you want keep the main thread alive in a loop that wait untill the glwindow has closed.
Which way would be the best and why...
Reply | Threaded
Open this post in threaded view
|

Re: Right method to exit/dispose on linux

gouessej
Administrator
If you want to exit cleanly, close the GLWindow before calling System.exit(). If you get a crash, please try to reproduce your problem with an existing test case and then fill a bug report.

You can put this loop into the main method too, it's a bit similar to what is done in Ardor3D.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Right method to exit/dispose on linux

elect
gouessej wrote
If you want to exit cleanly, close the GLWindow before calling System.exit(). If you get a crash, please try to reproduce your problem with an existing test case and then fill a bug report.

You can put this loop into the main method too, it's a bit similar to what is done in Ardor3D.
Where can I find the bug report?
Reply | Threaded
Open this post in threaded view
|

Re: Right method to exit/dispose on linux

gouessej
Administrator
Follow these instructions.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Right method to exit/dispose on linux

elect
Something like

public static void main(String[] args) {

        final OculusRoomTiny oculusRoomTiny = new OculusRoomTiny();
       
        while(oculusRoomTiny.getGlWindow().isRealized()){
           
        }
    }


isn't too much aggressive? Should I make it sleep every, let's say, 50ms?
Reply | Threaded
Open this post in threaded view
|

Re: Right method to exit/dispose on linux

gouessej
Administrator
Don't use Thread.sleep(). Maybe just call Thread.yield() instead but I'm not sure it is absolutely necessary. I call none of them in the main loop of TUER. I still don't understand your obstinacy to avoid using GLEventListener, it doesn't simplify anything and those who will read your code may think that's the way to go whereas it isn't.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Right method to exit/dispose on linux

elect
gouessej wrote
Don't use Thread.sleep(). Maybe just call Thread.yield() instead but I'm not sure it is absolutely necessary. I call none of them in the main loop of TUER. I still don't understand your obstinacy to avoid using GLEventListener, it doesn't simplify anything and those who will read your code may think that's the way to go whereas it isn't.
No wait, I don't get what you mean..

I guess you misunderstood obstinacy by nubity ^^
Reply | Threaded
Open this post in threaded view
|

Re: Right method to exit/dispose on linux

jmaasing
The following works very good for me, the animator will make sure the display callback is called to let me update the display.


public class NewtWindow {

        public static void main(String... args) {
                NewtWindow app = new NewtWindow();
                app.run();
        }

        private void run() {
                final GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL4));
                caps.setBackgroundOpaque(true);
                caps.setDoubleBuffered(true);
                GLWindow glWindow = GLWindow.create(caps);
                glWindow.setSize(1024, 768);
                glWindow.setUndecorated(false);
                glWindow.setPointerVisible(true);
                glWindow.setVisible(true);
                glWindow.setDefaultCloseOperation(WindowClosingProtocol.WindowClosingMode.DISPOSE_ON_CLOSE);

                Animator animator = new Animator(glWindow);
                glWindow.addGLEventListener(new GLEventListener() {
                        @Override
                        public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
                                System.out.println("Reshape <" + width + "," + height + ">");
                        }

                        @Override
                        public void init(GLAutoDrawable drawable) {
                                System.out.println("Init");
                        }

                        @Override
                        public void dispose(GLAutoDrawable drawable) {
                                animator.stop() ;
                                System.out.println("Dispose");
                        }

                        @Override
                        public void display(GLAutoDrawable drawable) {
                                System.out.println("Display");
                        }
                });

                animator.start();

        }
}
Reply | Threaded
Open this post in threaded view
|

Re: Right method to exit/dispose on linux

elect
Ah, I got it, he meant the animator then..

Thanks :)
Reply | Threaded
Open this post in threaded view
|

Re: Right method to exit/dispose on linux

gouessej
Administrator
In reply to this post by elect
elect wrote
gouessej wrote
Don't use Thread.sleep(). Maybe just call Thread.yield() instead but I'm not sure it is absolutely necessary. I call none of them in the main loop of TUER. I still don't understand your obstinacy to avoid using GLEventListener, it doesn't simplify anything and those who will read your code may think that's the way to go whereas it isn't.
No wait, I don't get what you mean..

I guess you misunderstood obstinacy by nubity ^^
http://www.thefreedictionary.com/obstinacy
Julien Gouesse | Personal blog | Website
12