diff --git a/android/gltfio-android/src/main/java/com/google/android/filament/gltfio/Animator.java b/android/gltfio-android/src/main/java/com/google/android/filament/gltfio/Animator.java index 6add0c5d6115..9e9f2ffa4343 100644 --- a/android/gltfio-android/src/main/java/com/google/android/filament/gltfio/Animator.java +++ b/android/gltfio-android/src/main/java/com/google/android/filament/gltfio/Animator.java @@ -37,29 +37,27 @@ */ public class Animator { private long mNativeObject; - private Boolean mIsOwner = false; + private boolean mIsOwner = false; Animator(long nativeObject) { mNativeObject = nativeObject; } + /** + * Creates an Animator that can manipulate animations in the provided FilamentAsset. + *

+ * Important: This Animator manages native resources that must be + * explicitly freed by calling {@link #destroy()} when no longer needed. Failing to + * call {@link #destroy()} will result in native memory leaks. + *

+ * + * @param asset The FilamentAsset containing the animations + * @param instance The FilamentInstance to animate + */ public Animator(FilamentAsset asset, FilamentInstance instance) { mNativeObject = nCreateAnimatorFromAssetAndInstance(asset.getNativeObject(), instance.getNativeObject()); mIsOwner = true; } - - @Override - public void finalize() { - try { - super.finalize(); - } catch (Throwable t) { // Ignore - } finally { - if (mIsOwner) { - nDestroyAnimator(mNativeObject); - mNativeObject = 0; - } - } - } /** * 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) { return nGetAnimationName(getNativeObject(), animationIndex); } + /** + * Explicitly destroys this Animator and frees all its associated native resources. + * The object will be unusable after this call. + */ + public void destroy() { + if (mNativeObject != 0) { + if (mIsOwner) { + nDestroyAnimator(mNativeObject); + } + mNativeObject = 0; + } + } + long getNativeObject() { if (mNativeObject == 0) { throw new IllegalStateException("Using Animator on destroyed asset");