Skip to content

Commit bf89e17

Browse files
ninjzmentholyspirit
authored andcommitted
Refactor Animator class to use primitive boolean and add destroy method for resource management
1 parent d9e3bba commit bf89e17

File tree

1 file changed

+25
-14
lines changed
  • android/gltfio-android/src/main/java/com/google/android/filament/gltfio

1 file changed

+25
-14
lines changed

android/gltfio-android/src/main/java/com/google/android/filament/gltfio/Animator.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,27 @@
3737
*/
3838
public class Animator {
3939
private long mNativeObject;
40-
private Boolean mIsOwner = false;
40+
private boolean mIsOwner = false;
4141

4242
Animator(long nativeObject) {
4343
mNativeObject = nativeObject;
4444
}
4545

46+
/**
47+
* Creates an Animator that can manipulate animations in the provided FilamentAsset.
48+
* <p>
49+
* <strong>Important:</strong> This Animator manages native resources that must be
50+
* explicitly freed by calling {@link #destroy()} when no longer needed. Failing to
51+
* call {@link #destroy()} will result in native memory leaks.
52+
* </p>
53+
*
54+
* @param asset The FilamentAsset containing the animations
55+
* @param instance The FilamentInstance to animate
56+
*/
4657
public Animator(FilamentAsset asset, FilamentInstance instance) {
4758
mNativeObject = nCreateAnimatorFromAssetAndInstance(asset.getNativeObject(), instance.getNativeObject());
4859
mIsOwner = true;
4960
}
50-
51-
@Override
52-
public void finalize() {
53-
try {
54-
super.finalize();
55-
} catch (Throwable t) { // Ignore
56-
} finally {
57-
if (mIsOwner) {
58-
nDestroyAnimator(mNativeObject);
59-
mNativeObject = 0;
60-
}
61-
}
62-
}
6361

6462
/**
6563
* Applies rotation, translation, and scale to entities that have been targeted by the given
@@ -145,6 +143,19 @@ public String getAnimationName(@IntRange(from = 0) int animationIndex) {
145143
return nGetAnimationName(getNativeObject(), animationIndex);
146144
}
147145

146+
/**
147+
* Explicitly destroys this Animator and frees all its associated native resources.
148+
* The object will be unusable after this call.
149+
*/
150+
public void destroy() {
151+
if (mNativeObject != 0) {
152+
if (mIsOwner) {
153+
nDestroyAnimator(mNativeObject);
154+
}
155+
mNativeObject = 0;
156+
}
157+
}
158+
148159
long getNativeObject() {
149160
if (mNativeObject == 0) {
150161
throw new IllegalStateException("Using Animator on destroyed asset");

0 commit comments

Comments
 (0)