[Android] Assert refcount == 0 on finalize

Romain Vimont git at videolan.org
Wed Jul 18 10:26:33 CEST 2018


vlc-android | branch: 3.0.x | Romain Vimont <rom1v at videolabs.io> | Tue Jul 17 19:45:32 2018 +0200| [fd3c2c7b57ee8384c9cb71d43281bbbcb3d44778] | committer: Geoffrey Métais

Assert refcount == 0 on finalize

VLCObject manages a refcount to release native resources. If the Java
object is finalized, this is an error if the refcount is not 0
(native resources have not been released as expected).

Signed-off-by: Geoffrey Métais <geoffrey.metais at gmail.com>
(cherry picked from commit afea771c071cb34e32730bfc94a51a1323985a10)

> https://code.videolan.org/videolan/vlc-android/commit/fd3c2c7b57ee8384c9cb71d43281bbbcb3d44778
---

 libvlc/src/org/videolan/libvlc/VLCObject.java | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/libvlc/src/org/videolan/libvlc/VLCObject.java b/libvlc/src/org/videolan/libvlc/VLCObject.java
index a22195127..1144434cf 100644
--- a/libvlc/src/org/videolan/libvlc/VLCObject.java
+++ b/libvlc/src/org/videolan/libvlc/VLCObject.java
@@ -91,6 +91,12 @@ abstract class VLCObject<T extends VLCEvent> {
         }
     }
 
+    @Override
+    protected synchronized void finalize() {
+        if (!isReleased())
+            throw new AssertionError("VLCObject (" + getClass().getName() + ") finalized but not natively released (" + mNativeRefCount + " refs)");
+    }
+
     /**
      * Set an event listener.
      * Events are sent via the android main thread.



More information about the Android mailing list