Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DisposalHelper class #7334

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
e95caf1
Add DisposalHelper
Anton-Samarskyi Feb 4, 2024
c7ab5de
Replace Collection with Iterable in DisposalHelper
Anton-Samarskyi Feb 4, 2024
140ced8
Update Skin with DisposalHelper usage
Anton-Samarskyi Feb 4, 2024
b584bc2
Add full null safety to the DisposalHelper
Anton-Samarskyi Feb 4, 2024
23ba95e
Add minor javadoc fix
Anton-Samarskyi Feb 4, 2024
c06c630
Add minor code style fix
Anton-Samarskyi Feb 4, 2024
5a4cc9c
Make exceptions public in GdxComplexDisposalException
Anton-Samarskyi Feb 4, 2024
9843c83
Add minor fix
Anton-Samarskyi Feb 4, 2024
f0f5066
Fix formatting
Anton-Samarskyi Feb 4, 2024
7d222f4
Fix javadoc formatting once again
Anton-Samarskyi Feb 4, 2024
6ccdf25
Add gdx StringBuilder usage (change import)
Anton-Samarskyi Feb 4, 2024
af9e55d
Remove StringBuilder import as it is in the same package
Anton-Samarskyi Feb 4, 2024
f21dd7f
Replace java lists with gdx arrays
Anton-Samarskyi Feb 4, 2024
940bf4d
Update gdx.gwt.xml
Anton-Samarskyi Feb 5, 2024
4ee5b1a
Merge branch 'master' into disposalHelper
Anton-Samarskyi Feb 17, 2024
8c7d69e
Merge branch 'master' into disposalHelper
Anton-Samarskyi Feb 19, 2024
429e6c5
Remove silent disposal
Anton-Samarskyi Feb 22, 2024
3e442e7
Add more usages of DisposalHelper
Anton-Samarskyi Feb 22, 2024
2c03454
Merge branch 'master' into disposalHelper
Anton-Samarskyi Mar 12, 2024
203c6e4
Merge branch 'master' into disposalHelper
Anton-Samarskyi Mar 17, 2024
e6d6f4d
Merge branch 'master' into disposalHelper
Anton-Samarskyi Mar 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions gdx/res/com/badlogic/gdx.gwt.xml
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,10 @@
<include name="utils/DataOutput.java"/>
<include name="utils/DelayedRemovalArray.java"/>
<include name="utils/Disposable.java"/>
<include name="utils/DisposalHelper.java"/>
<include name="utils/FloatArray.java"/>
<include name="utils/FlushablePool.java"/>
<include name="utils/GdxComplexDisposalException.java"/>
<exclude name="utils/GdxNativesLoader.java"/> <!-- Reason: Natives -->
<include name="utils/GdxRuntimeException.java"/>
<include name="utils/I18NBundle.java"/>
Expand Down
5 changes: 2 additions & 3 deletions gdx/src/com/badlogic/gdx/graphics/Mesh.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.badlogic.gdx.math.collision.BoundingBox;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.DisposalHelper;
import com.badlogic.gdx.utils.GdxRuntimeException;

/**
Expand Down Expand Up @@ -660,9 +661,7 @@ public void render (ShaderProgram shader, int primitiveType, int offset, int cou
/** Frees all resources associated with this Mesh */
public void dispose () {
if (meshes.get(Gdx.app) != null) meshes.get(Gdx.app).removeValue(this, true);
vertices.dispose();
if (instances != null) instances.dispose();
indices.dispose();
DisposalHelper.disposeAll(vertices, instances, indices);
}

/** Returns the first {@link VertexAttribute} having the given {@link Usage}.
Expand Down
4 changes: 2 additions & 2 deletions gdx/src/com/badlogic/gdx/graphics/g2d/SpriteCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.DisposalHelper;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.badlogic.gdx.utils.IntArray;

Expand Down Expand Up @@ -942,8 +943,7 @@ public void draw (int cacheID, int offset, int length) {

/** Releases all resources held by this SpriteCache. */
public void dispose () {
mesh.dispose();
if (shader != null) shader.dispose();
DisposalHelper.disposeAll(mesh, shader);
}

public Matrix4 getProjectionMatrix () {
Expand Down
6 changes: 4 additions & 2 deletions gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Skin.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.badlogic.gdx.scenes.scene2d.utils.TiledDrawable;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.DisposalHelper;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.Json.ReadOnlySerializer;
Expand Down Expand Up @@ -449,11 +450,12 @@ public void setEnabled (Actor actor, boolean enabled) {

/** Disposes the {@link TextureAtlas} and all {@link Disposable} resources in the skin. */
public void dispose () {
if (atlas != null) atlas.dispose();
DisposalHelper disposalHelper = new DisposalHelper(atlas);
for (ObjectMap<String, Object> entry : resources.values()) {
for (Object resource : entry.values())
if (resource instanceof Disposable) ((Disposable)resource).dispose();
if (resource instanceof Disposable) disposalHelper.add((Disposable)resource);
}
disposalHelper.dispose();
}

protected Json getJsonLoader (final FileHandle skinFile) {
Expand Down
116 changes: 116 additions & 0 deletions gdx/src/com/badlogic/gdx/utils/DisposalHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/

package com.badlogic.gdx.utils;

/** Util class for disposing resources. It's useful for disposing multiple resources. Null-safe: provided disposable resources
* could be null. There is a guarantee that all resources are disposed of, even when exception(s) occurs.
* @author Anton-Samarkyi */
public class DisposalHelper implements Disposable {
private final Array<Disposable> toDispose = new Array<>();
private Array<Exception> exceptions;

/** Constructs an empty DisposalHelper. */
public DisposalHelper () {
}

/** Constructs a DisposalHelper with disposableItems set to dispose.
* @param disposableItems items to dispose */
public DisposalHelper (Disposable... disposableItems) {
addAll(disposableItems);
}

/** Constructs a DisposalHelper with disposableItems set to dispose.
* @param disposableItems items to dispose */
public DisposalHelper (Iterable<Disposable> disposableItems) {
addAll(disposableItems);
}

/** Adds item to dispose.
* @param disposableItem item to dispose
* @return this helper */
public DisposalHelper add (Disposable disposableItem) {
toDispose.add(disposableItem);
return this;
}

/** Adds items to dispose.
* @param disposableItems items to dispose
* @return this helper */
public DisposalHelper addAll (Disposable... disposableItems) {
if (disposableItems == null) return this;
for (Disposable item : disposableItems) {
toDispose.add(item);
}
return this;
}

/** Adds items to dispose.
* @param disposableItems items to dispose
* @return this helper */
public DisposalHelper addAll (Iterable<Disposable> disposableItems) {
if (disposableItems == null) return this;
for (Disposable item : disposableItems) {
toDispose.add(item);
}
return this;
}

@Override
public void dispose () {
for (Disposable item : toDispose) {
if (item != null && item != this) {
try {
item.dispose();
} catch (Exception e) {
addException(e);
}
}
}

if (exceptions != null) {
if (exceptions.size == 1) {
Exception exception = exceptions.get(0);
if (exception instanceof RuntimeException) {
throw (RuntimeException)exception;
} else {
throw new GdxRuntimeException("Disposal exception", exception);
}
} else {
throw new GdxComplexDisposalException(exceptions);
}
}
}

private void addException (Exception e) {
if (exceptions == null) {
exceptions = new Array<>();
}
exceptions.add(e);
}

/** Disposes multiple items. There is a guarantee that all resources are disposed of, even when exception(s) occurs.
* @param disposableItems items to dispose */
public static void disposeAll (Disposable... disposableItems) {
new DisposalHelper(disposableItems).dispose();
}

/** Disposes multiple items. There is a guarantee that all resources are disposed of, even when exception(s) occurs.
* @param disposableItems items to dispose */
public static void disposeAll (Iterable<Disposable> disposableItems) {
new DisposalHelper(disposableItems).dispose();
}
}
41 changes: 41 additions & 0 deletions gdx/src/com/badlogic/gdx/utils/GdxComplexDisposalException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/

package com.badlogic.gdx.utils;

/** Runtime exception that contains array of exceptions occurred during the disposing process.
* @author Anton-Samarkyi */
public class GdxComplexDisposalException extends GdxRuntimeException {
private static final long serialVersionUID = 1L;
private static final String MESSAGE = "Multiple exceptions occurred during the disposal process: ";

public final Array<Exception> exceptions;

public GdxComplexDisposalException (Array<Exception> exceptions) {
super(composeMessage(exceptions));
this.exceptions = exceptions;
}

private static String composeMessage (Array<Exception> exceptions) {
StringBuilder builder = new StringBuilder(MESSAGE);

for (Exception exception : exceptions) {
builder.append(exception.toString()).append(", ");
}

return builder.substring(0, builder.length() - 2);
}
}