Pbuffer makeCurrent() fails after several iterations

I’m trying to re-use a Pbuffer (because making new ones is too slow) but it is failing after several iterations. Here is some really reduced code to demonstrate.


public static void main(String[] args) {
   GLPbuffer pBuffer = GLDrawableFactory.getFactory().createGLPbuffer(new GLCapabilities(), null, 512, 512, null);
   GLContext context = pBuffer.getContext();
   for(int c=0;c<10000;c++) {
        context.makeCurrent();
        context.release();
	System.out.println(c);
   }
}

This seems to fail after 4k or so iterations on my box. If I add other (useful) commands in the loop it will fail faster. Sometimes in as few as 4 or 5 iterations. The number of iteration before failure is not consistent. The size of the Pbuffer doesn’t seem to affect the failure rate.


Exception in thread "main" javax.media.opengl.GLException: Error making context current: 0
	at com.sun.opengl.impl.windows.WindowsGLContext.makeCurrentImpl(WindowsGLContext.java:169)
	at com.sun.opengl.impl.windows.WindowsPbufferGLContext.makeCurrentImpl(WindowsPbufferGLContext.java:102)
	at com.sun.opengl.impl.GLContextImpl.makeCurrent(GLContextImpl.java:134)
	at com.sri.penrad.analystdisplay.wwj.dstile.UserImportedCache.main(UserImportedCache.java:424)

Can anyone offer help?

WinXP
JSR-231 1.1.1 - May 22

I tried your code, and repeated the problem. This version works ok on my system. Sorry for the lack of explanation, but this should be enough to indicate what you need to read up.

	public static void main(String[] args) {
		GLPbuffer pBuffer = GLDrawableFactory.getFactory().createGLPbuffer(new GLCapabilities(), null, 512, 512, null);
		final GLContext context = pBuffer.getContext();
		SwingUtilities.invokeLater(new Runnable(){
			public void run() {
				for (int c = 0; c < 10000; c++) {
					context.makeCurrent();
					context.release();
					System.out.println(c);
				}
			}
		});
	}

Interesting, didn’t realize that there was a dependency on the AWT thread

I suppose this is enough to get me going…

Thanks!

You can also use Threading.invokeOnOpenGLThread but actually this is the same as calling SwingUtilities.invokeAndWait.

I noticed that this will still occasionally fail. Seems to be related to what else is going happening with my computer. For example photoshop or firefox starting up seems to cause issues with the PBuffer.

But generally, the crash rate is low enough that I seem to be able to handle and recover.