[Android] [PATCH] Assert refcount == 0 on finalize

Romain Vimont rom1v at videolabs.io
Tue Jul 17 19:45:32 CEST 2018


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).
---
 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.
-- 
2.18.0



More information about the Android mailing list