aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKenneth Russel <[email protected]>2005-05-02 06:06:08 +0000
committerKenneth Russel <[email protected]>2005-05-02 06:06:08 +0000
commitbf3544c279ea1734dfed827c1fdbe7fa7ca9dbad (patch)
treeb6e492e66da7f3ad6ffda925004f18d515a7a041 /src
parent24c5dd419e8094b030a55ea86acee4dce0c61f06 (diff)
Fixed Issue 160: Resource leaks in GLJPanel
Added addNotify and removeNotify to GLJPanel which clean up the associated OpenGL contexts and other resources. Extended JRefract demo to stress GLJPanel creation and destruction. New code appears to be correct. Can see resource leaks when the bunny is loaded over and over, but believe these are probably due to allocation of large NIO buffers that are not getting finalized promptly. Stressing the Gears demo with both the pbuffer and software rendering paths shows that the OpenGL resources are being reclaimed properly. git-svn-id: file:///usr/local/projects/SUN/JOGL/git-svn/svn-server-sync/jogl/trunk@263 232f8b59-042b-4e1e-8c03-345bb8c30851
Diffstat (limited to 'src')
-rw-r--r--src/net/java/games/jogl/GLCanvas.java8
-rw-r--r--src/net/java/games/jogl/GLJPanel.java51
2 files changed, 57 insertions, 2 deletions
diff --git a/src/net/java/games/jogl/GLCanvas.java b/src/net/java/games/jogl/GLCanvas.java
index 17aed5bea..e0065153f 100644
--- a/src/net/java/games/jogl/GLCanvas.java
+++ b/src/net/java/games/jogl/GLCanvas.java
@@ -59,6 +59,8 @@ import net.java.games.jogl.impl.*;
public final class GLCanvas extends Canvas implements GLDrawable {
+ protected static final boolean DEBUG = Debug.debug("GLCanvas");
+
private GLDrawableHelper drawableHelper = new GLDrawableHelper();
private GLContext context;
@@ -90,6 +92,9 @@ public final class GLCanvas extends Canvas implements GLDrawable {
public void addNotify() {
super.addNotify();
context.setRealized();
+ if (DEBUG) {
+ System.err.println("GLCanvas.addNotify()");
+ }
}
/** Overridden from Canvas; used to indicate that it's no longer
@@ -97,6 +102,9 @@ public final class GLCanvas extends Canvas implements GLDrawable {
public void removeNotify() {
context.destroy();
super.removeNotify();
+ if (DEBUG) {
+ System.err.println("GLCanvas.removeNotify()");
+ }
}
/** Overridden from Canvas; causes {@link GLDrawableHelper#reshape}
diff --git a/src/net/java/games/jogl/GLJPanel.java b/src/net/java/games/jogl/GLJPanel.java
index 229cc3446..d21081dfe 100644
--- a/src/net/java/games/jogl/GLJPanel.java
+++ b/src/net/java/games/jogl/GLJPanel.java
@@ -67,7 +67,10 @@ import net.java.games.jogl.impl.*;
them. */
public final class GLJPanel extends JPanel implements GLDrawable {
+ protected static final boolean DEBUG = Debug.debug("GLJPanel");
+
private GLDrawableHelper drawableHelper = new GLDrawableHelper();
+ private volatile boolean isInitialized;
// Data used for either pbuffers or pixmap-based offscreen surfaces
private GLCapabilities offscreenCaps;
@@ -114,11 +117,13 @@ public final class GLJPanel extends JPanel implements GLDrawable {
offscreenCaps.setDoubleBuffered(false);
this.chooser = chooser;
this.shareWith = shareWith;
-
- initialize();
}
public void display() {
+ if (!isInitialized) {
+ return;
+ }
+
if (EventQueue.isDispatchThread()) {
// Want display() to be synchronous, so call paintImmediately()
paintImmediately(0, 0, getWidth(), getHeight());
@@ -137,6 +142,10 @@ public final class GLJPanel extends JPanel implements GLDrawable {
GLEventListener#display}. Should not be invoked by applications
directly. */
public void paintComponent(Graphics g) {
+ if (!isInitialized) {
+ return;
+ }
+
updater.setGraphics(g);
if (!hardwareAccelerationDisabled) {
if (!pbufferInitializationCompleted) {
@@ -161,6 +170,37 @@ public final class GLJPanel extends JPanel implements GLDrawable {
}
}
+ public void addNotify() {
+ super.addNotify();
+ initialize();
+ if (DEBUG) {
+ System.err.println("GLJPanel.addNotify()");
+ }
+ }
+
+ /** Overridden from JPanel; used to indicate that it's no longer
+ safe to have an OpenGL context for the component. */
+ public void removeNotify() {
+ if (DEBUG) {
+ System.err.println("GLJPanel.removeNotify()");
+ }
+ if (!hardwareAccelerationDisabled) {
+ if (pbuffer != null) {
+ pbuffer.destroy();
+ }
+ if (toplevel != null) {
+ toplevel.dispose();
+ }
+ pbuffer = null;
+ heavyweight = null;
+ toplevel = null;
+ } else {
+ offscreenContext.destroy();
+ }
+ isInitialized = false;
+ super.removeNotify();
+ }
+
/** Overridden from Canvas; causes {@link GLDrawableHelper#reshape}
to be called on all registered {@link GLEventListener}s. Called
automatically by the AWT; should not be invoked by applications
@@ -168,6 +208,10 @@ public final class GLJPanel extends JPanel implements GLDrawable {
public void reshape(int x, int y, int width, int height) {
super.reshape(x, y, width, height);
+ if (!isInitialized) {
+ return;
+ }
+
// Move all reshape requests onto AWT EventQueue thread
final int fx = x;
final int fy = y;
@@ -393,6 +437,7 @@ public final class GLJPanel extends JPanel implements GLDrawable {
}
}
});
+ isInitialized = true;
return;
} else {
// If the heavyweight reports that it can't create an
@@ -415,6 +460,8 @@ public final class GLJPanel extends JPanel implements GLDrawable {
}
}, true, initAction);
}
+
+ isInitialized = true;
}
class Updater implements GLEventListener {