Com.jme.input.Mouse - memory leak when changing cursors

In com.jme.input.Mouse, every time you set a new RenderState that is a TextureState, the mouse will reinitialize the quad. The problem is that quad initialization creates new objects. So changing cursors often results in GC kicking in.



I realize its a very very minor thing and the cursor will not be changed often enough to create major problems. (The reason I found it was stress-testing method that changes cursors). However the fix for it is very simple…


  1. add a line "initialize(0, 0);" to the Mouse constructor,
  2. in setRenderState(RenderState rs), change "initialize(imageWidth, imageHeight);" to "resize(imageWidth, imageHeight);"



    Thats it… just two lines.



    Here is the code after modifications:


    public Mouse(String name) {
        super(name, 32, 32);
        setCullMode(SceneElement.CULL_NEVER);
        setRenderQueueMode(Renderer.QUEUE_ORTHO);
        setZOrder(Integer.MIN_VALUE);
        setLightCombineMode(LightState.OFF);
        setTextureCombineMode(TextureState.REPLACE);
   
        initialize(0, 0);
    }

    public RenderState setRenderState(RenderState rs) {
        if (rs.getType() == RenderState.RS_TEXTURE) {
            hasCursor = true;
            imageHeight = ((TextureState) rs).getTexture().getImage()
                    .getHeight();
            imageWidth = ((TextureState) rs).getTexture().getImage().getWidth();
            resize(imageWidth, imageHeight);
            hotSpotOffset.set(-imageWidth / 2, imageHeight / 2, 0);
        }
        return super.setRenderState(rs);
    }

Just came across this while scanning older unread posts (I have 11 pages of such… :frowning: )  Thanks for catching this lex.

Thank you for introducing the changes!

I probably shouldn't post this so your unread posts don't increase in numbers… 8)

in such cases, probably the best thing you can do to ease the dev's work is using the issue tracker :wink: