TextRenderer and memory issues

Posted by r.jaoui on
URL: https://forum.jogamp.org/TextRenderer-and-memory-issues-tp4041129.html

Hello !

I'm currently working a memory leak that I can't understand.

It has to do with the TextRenderer class. Basically, in a specific JOGL application, inside a panel listener I have a TextRenderer Object that manages... well, text rendering.

The issue is that this Object isn't created in the init() method since I need to be able to render with different fonts, that I can't know in advance. For now, I have the following function that "updates" the TextRenderer (and that may run at every frame of the application) :

private void initTextRenderer() {
        if(context.listener.textRenderer == null) {
                context.listener.textRenderer = new TextRenderer(currentStyle.currentFont.getFont(), true, true);
        }
        //The else condition is simplified here, in the real code it checks if the fonts are actually the same but not
        //necessarily the same instance...
        else if(!context.listener.textRenderer.getFont().equals(currentStyle.currentFont.getFont())) {
                context.listener.textRenderer.dispose();
                context.listener.textRenderer = new TextRenderer(currentStyle.currentFont.getFont(), true, true);
        }
}

The issue is then that creating a new TextRenderer, even if disposing of the previous one beforehand, seems to cause a memory leak that I can't wrap my head around. This memory leak is pretty slow and only starts occuring after a few seconds (although at this point this method has already been called hundreds of times).

Is there a way to modify this code so that I can update the TextRenderer (or even just update the Font Object it encapsulates) to be able to use it as a text renderer for multiple fonts (again here it is important to emphasize that I can't have a TextRenderer for each font since i dont know in advance the number of fonts, and this number may be strictly increasing as time goes by...).

If not, is there another way to render text in JOGL that wouldn't suffer from this problem ?

Thanks !