Skip to content

Commit

Permalink
fix memory leak when disposing SceneAsset
Browse files Browse the repository at this point in the history
  • Loading branch information
mgsx-dev committed Oct 24, 2022
1 parent 7b6253d commit 51793cd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions gltf/src/net/mgsx/gltf/loaders/shared/GLTFLoaderBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class GLTFLoaderBase implements Disposable {
private static final ObjectSet<Material> materialSet = new ObjectSet<Material>();
private static final ObjectSet<MeshPart> meshPartSet = new ObjectSet<MeshPart>();
private static final ObjectSet<Mesh> meshSet = new ObjectSet<Mesh>();
private final ObjectSet<Mesh> loadedMeshes = new ObjectSet<Mesh>();

private final Array<Camera> cameras = new Array<Camera>();
private final Array<BaseLight> lights = new Array<BaseLight>();
Expand Down Expand Up @@ -141,6 +142,9 @@ public SceneAsset load(DataFileResolver dataFileResolver, boolean withData){
scene.model.animations.addAll(animationLoader.animations);
}

copy(loadedMeshes, model.meshes = new Array<Mesh>());
loadedMeshes.clear();

return model;
}catch(RuntimeException e){
dispose();
Expand Down Expand Up @@ -170,6 +174,10 @@ public void dispose() {
for(SceneModel scene : scenes){
scene.dispose();
}
for(Mesh mesh : loadedMeshes){
mesh.dispose();
}
loadedMeshes.clear();
}

private void loadScenes() {
Expand Down Expand Up @@ -213,6 +221,8 @@ private SceneModel loadScene(GLTFScene gltfScene)
// collect data references to store in model
collectData(sceneModel.model, sceneModel.model.nodes);

loadedMeshes.addAll(meshSet);

copy(meshSet, sceneModel.model.meshes);
copy(meshPartSet, sceneModel.model.meshParts);
copy(materialSet, sceneModel.model.materials);
Expand Down
9 changes: 9 additions & 0 deletions gltf/src/net/mgsx/gltf/scene3d/scene/SceneAsset.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.mgsx.gltf.scene3d.scene;

import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g3d.model.Animation;
import com.badlogic.gdx.utils.Array;
Expand All @@ -24,6 +25,9 @@ public class SceneAsset implements Disposable
/** Keep track of loaded texture in order to dispose them. Textures handled by AssetManager are excluded. */
public Array<Texture> textures;

/** Keep track of loaded meshes in order to dispose them. */
public Array<Mesh> meshes;

@Override
public void dispose() {
if(scenes != null){
Expand All @@ -36,5 +40,10 @@ public void dispose() {
texture.dispose();
}
}
if(meshes != null){
for(Mesh mesh : meshes){
mesh.dispose();
}
}
}
}

0 comments on commit 51793cd

Please sign in to comment.