Fug*_*ugo 1 java textures libgdx
我想知道这段代码是否会导致内存泄漏?因为我还是不知道什么时候应该处理一个纹理.纹理应该放在方法的最后?或未使用后单独处理?
private void loadAssets() {
Texture texture = new Texture(Gdx.files.internal("data/controls.png"));
TextureRegion[] buttons = TextureRegion.split(texture, 64, 64)[0];
left = buttons[0];
right = buttons[1];
jump = buttons[2];
cubeControl = buttons[3];
cubeFollow = TextureRegion.split(texture, 64, 64)[1][2];
dpad = new TextureRegion(texture, 0, 64, 128, 128);
batch = new SpriteBatch();
batch.getProjectionMatrix().setToOrtho2D(0, 0, 480, 320);
}
Run Code Online (Sandbox Code Playgroud)
我真的不明白 为什么Java中仍然存在内存泄漏?
看看TextureRegion它的来源,它保留了对它的引用,Texture但并没有真正取得它的"所有权" - 它永远不会自己处理它.
通过走出去,你不应该在方法的最后处置它,但你应该 处理它时cubeFollow,cubeControl并dpad都与完成-我怀疑当实例本身与完成会.(这可能意味着也可能不意味着你需要一个终结器 - 这取决于你是否对这个对象的生命周期有一些控制权.)
请注意,您不需要单独的字段来存储对Texture您的引用- 您可以从中dpad使用它dpad.getTexture().
| 归档时间: |
|
| 查看次数: |
525 次 |
| 最近记录: |