渲染纹理导致内存泄漏(Rendering textures causing memory leak)

编程入门 行业动态 更新时间:2024-10-19 20:32:13
渲染纹理导致内存泄漏(Rendering textures causing memory leak)

在我的游戏中,我有这个代码。 它呈现一个用作按钮的纹理:

private void drawStart(){ startTexture = new Texture(Gdx.files.internal("start.png")); startTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear); stageStart = new Stage(); stageStart.clear(); buttonStart = new Image(startTexture); buttonStart.setX(10); buttonStart.setY(Gdx.graphics.getHeight()/2.75f); buttonStart.setWidth(Gdx.graphics.getWidth()/4); buttonStart.setHeight(Gdx.graphics.getHeight()/4); Gdx.input.setInputProcessor(stageStart); buttonStart.addListener(new ClickListener() { public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { currentState = GameState.RESET; startTexture.dispose(); stageStart.dispose(); return true; } }); stageStart.addActor(buttonStart); stageStart.draw(); startTexture.dispose();

}

但是,每当我把drawStart(); 在我的渲染方法中,Java堆和原生堆每10秒缓慢增加1。 因此,如果用户在菜单上离开游戏约5分钟,游戏将在他们的手机上崩溃。 我测试了它,它只在渲染纹理时才会出现。

我很感激任何帮助解决这个问题。 我已经尝试了一个if语句,表明如果render = 0,渲染纹理然后设置渲染1但是没有用。

Inside my game I have this code. It renders a texture that serve as a button:

private void drawStart(){ startTexture = new Texture(Gdx.files.internal("start.png")); startTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear); stageStart = new Stage(); stageStart.clear(); buttonStart = new Image(startTexture); buttonStart.setX(10); buttonStart.setY(Gdx.graphics.getHeight()/2.75f); buttonStart.setWidth(Gdx.graphics.getWidth()/4); buttonStart.setHeight(Gdx.graphics.getHeight()/4); Gdx.input.setInputProcessor(stageStart); buttonStart.addListener(new ClickListener() { public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { currentState = GameState.RESET; startTexture.dispose(); stageStart.dispose(); return true; } }); stageStart.addActor(buttonStart); stageStart.draw(); startTexture.dispose();

}

However, whenever I put drawStart(); into my render method, the Java Heap and Native Heap slowly increases by 1 every 10 seconds. So, if the user leaves the game on the menu for about 5 minutes the game will crash on their phone. I've tested it and it only occurs when the texture is rendered.

I would appreciate any help on fixing this. I have tried an if statement that states if rendered = 0, render the texture then set rendered 1 but that didn't work.

最满意答案

这可能对你有帮助。 您只需要在渲染中绘制。 所以现在你可以在你的渲染方法中放置drawStart(),它只会绘制舞台,同时让屏幕不要忘记调用dispose。

private void drawStart(){ stageStart.draw(); } public void initialize() { startTexture = new Texture(Gdx.files.internal("start.png")); startTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear); stageStart = new Stage(); stageStart.clear(); buttonStart = new Image(startTexture); buttonStart.setX(10); buttonStart.setY(Gdx.graphics.getHeight()/2.75f); buttonStart.setWidth(Gdx.graphics.getWidth()/4); buttonStart.setHeight(Gdx.graphics.getHeight()/4); Gdx.input.setInputProcessor(stageStart); buttonStart.addListener(new ClickListener() { public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { currentState = GameState.RESET; startTexture.dispose(); stageStart.dispose(); return true; } }); stageStart.addActor(buttonStart); } public void dispose() { startTexture.dispose(); }

This might help you. You only need draw in your render. So now you can put drawStart() in your render method which will only draw the stage, while leaving screen dont forget to call dispose.

private void drawStart(){ stageStart.draw(); } public void initialize() { startTexture = new Texture(Gdx.files.internal("start.png")); startTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear); stageStart = new Stage(); stageStart.clear(); buttonStart = new Image(startTexture); buttonStart.setX(10); buttonStart.setY(Gdx.graphics.getHeight()/2.75f); buttonStart.setWidth(Gdx.graphics.getWidth()/4); buttonStart.setHeight(Gdx.graphics.getHeight()/4); Gdx.input.setInputProcessor(stageStart); buttonStart.addListener(new ClickListener() { public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { currentState = GameState.RESET; startTexture.dispose(); stageStart.dispose(); return true; } }); stageStart.addActor(buttonStart); } public void dispose() { startTexture.dispose(); }

更多推荐

本文发布于:2023-08-06 14:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1450334.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:纹理   内存   Rendering   textures   memory

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!