Memory leak in TextRenderer ?

I probably found a memory leak in TextRenderer class in JOGL 1.1.1-RC8.

It appears when many TextRender are allocated. Even if the dispose method is called, all the ressources allocated by a TextRenderer aren’t freed.
I created a small test case:


import java.awt.Font;
import java.awt.Frame;
import java.awt.event.*;
import java.awt.geom.*;

import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.opengl.util.*;
import com.sun.opengl.util.j2d.*;


/** Shows how to place 2D text in 3D using the TextRenderer. */

public class TestTextRenderer implements GLEventListener {
  private GLU glu = new GLU();
  
  public static void main(String[] args) {
    Frame frame = new Frame("Text Cube");

    GLJPanel canvas = new GLJPanel();
    final TestTextRenderer demo = new TestTextRenderer();

    canvas.addGLEventListener(demo);
    frame.add(canvas);

    frame.setSize(640, 480);
    final Animator animator = new Animator(canvas);
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          // Run this on another thread than the AWT event queue to
          // make sure the call to Animator.stop() completes before
          // exiting
          new Thread(new Runnable() {
              public void run() {
                animator.stop();
                System.exit(0);
              }
            }).start();
        }
      });
    frame.show();
    animator.start();
  }

  public void init(GLAutoDrawable drawable) {
    // normally renderer should be created here
  }

  public void display(GLAutoDrawable drawable) {
    GL gl = drawable.getGL();


    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
    gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glPushMatrix();
    glu.gluLookAt(0.5, 0.5, 10,
                  0.5, 0.5, 0,
                  0, 1, 0);
    gl.glColor3d(1.0,1.0,1.0);

    // draw text
    TextRenderer renderer = new TextRenderer(new Font("Dialog", Font.PLAIN, 12));
    renderer.setColor(1.0f, 1.0f, 1.0f, 1.0f);
    renderer.begin3DRendering();
        
    renderer.draw3D("Hello JOGL!!!",
                    (float) Math.random(),(float) Math.random(), (float) Math.random(), 0.01f);
                    
    renderer.end3DRendering();
    
    gl.glPopMatrix();

    // should get memory back
    renderer.flush();
    renderer.dispose();
    

    
  }

  public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
    GL gl = drawable.getGL();
    gl.glMatrixMode(GL.GL_PROJECTION);
    gl.glLoadIdentity();
    glu.gluPerspective(15, (float) width / (float) height, 5, 15);
  }

  public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {}

}


Basically, it is just printing some text on the screen. I agree that it is useless and time consuming to create a new text renderer each time display is called. However, it might be needed to recreate a new textrenderer outside the init method, especially to change the font.

If you run the code above, you’ll see that the programm use more and more memory.

But maybe I’m using TextRenderer in an incorrect way.
If not I will report the bug on the bug tracker.

Thank you,
Jean-Baptiste

I have faced this memory leak as well and already raised this issue in another thread. ! (JOGL Memory Leaks).

Thank you for your answer but I’m not sure to experience the same problem you have.

I use some display lists in my application but didn’t notice any memory leaks (I only use a few, not millions, so maybe I was not able to detect them).
I’m just having troubles with the TextRenderer.
However, I agree with you, TextRenderer is very slow. I guess I’m not using it in the correct way since it appears that some people are able to render thousands of Strings (I read it on a post on this forum).

But maybe the display lists memory leaks and TextRenderer ones are related.

Jean-Baptiste

While it’s possible there’s a resource leak, you’re not using the TextRenderer as intended. You’re supposed to allocate one in your init() method and use it over and over again. Please see the source code for the JOGL demos.

I know, but I need to use several fonts in my application. So I need to recreate a new TextRenderer each time.
It would be great to add a setFont option in TextRenderer (as requested in an other post) http://www.java-gaming.org/forums/index.php?topic=18080.0).
I added the suggestion in the issue tracker (issue 348).

Jean-Baptiste