Scirenderer: Fix memleak: textures were not correctly disposed 26/12626/2
Calixte DENIZET [Fri, 20 Sep 2013 14:37:26 +0000 (16:37 +0200)]
Change-Id: I080adac89188d3db3df4694f5840774d75a5a4e5

scilab/modules/scirenderer/src/org/scilab/forge/scirenderer/implementation/jogl/texture/JoGLTextureManager.java

index 83ba612..1312bbb 100644 (file)
@@ -52,7 +52,6 @@ import java.util.Set;
 public class JoGLTextureManager implements TextureManager {
 
     private final Set<JoGLTexture> allTextures = new HashSet<JoGLTexture>();
-    private final Set<JoGLTexture> toDispose = new HashSet<JoGLTexture>();
     JoGLCanvas canvas;
 
     public JoGLTextureManager(JoGLCanvas canvas) {
@@ -142,9 +141,7 @@ public class JoGLTextureManager implements TextureManager {
     public void dispose(Texture texture) {
         if ((texture instanceof JoGLTexture) && (allTextures.contains((JoGLTexture) texture))) {
             allTextures.remove((JoGLTexture) texture);
-            synchronized (toDispose) {
-                toDispose.add((JoGLTexture) texture);
-            }
+            ((JoGLTexture) texture).dispose();
         }
     }
 
@@ -160,6 +157,7 @@ public class JoGLTextureManager implements TextureManager {
         private double tfactor = 1;
         private ByteBuffer buffer;
         private TextureDataProvider.ImageType previousType;
+        private JoGLDrawingTools drawingTools;
 
         /**
          * Default constructor.
@@ -177,6 +175,10 @@ public class JoGLTextureManager implements TextureManager {
          * @throws SciRendererException if the texture is invalid.
          */
         public synchronized void bind(JoGLDrawingTools drawingTools) throws SciRendererException {
+            if (this.drawingTools == null) {
+                this.drawingTools = this.drawingTools;
+            }
+
             GL2 gl = drawingTools.getGl().getGL2();
             if (isValid()) {
                 checkData(drawingTools);
@@ -216,16 +218,6 @@ public class JoGLTextureManager implements TextureManager {
          * @throws SciRendererException if the texture is too big.
          */
         private synchronized void checkData(JoGLDrawingTools drawingTools) throws SciRendererException {
-            synchronized (toDispose) {
-                if (!toDispose.isEmpty()) {
-                    final GL2 gl = drawingTools.getGl().getGL2();
-                    for (JoGLTexture jt : toDispose) {
-                        jt.releaseTextures(gl);
-                    }
-                    toDispose.clear();
-                }
-            }
-
             if (isValid() && !upToDate) {
                 GL2 gl = drawingTools.getGl().getGL2();
 
@@ -319,6 +311,12 @@ public class JoGLTextureManager implements TextureManager {
             }
         }
 
+        public void dispose() {
+            if (drawingTools != null) {
+                releaseTextures(drawingTools.getGl().getGL2());
+            }
+        }
+
         public boolean preDraw(JoGLDrawingTools drawingTools) throws SciRendererException {
             checkData(drawingTools);