From 313b27cfcf5aa6ea6c16eaec0e6a8452e998d481 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sat, 14 May 2022 04:45:20 +0200 Subject: C++/JNI Lifecycle Fix: BTManager must be passed as shared_ptr<> and hold as such in BTAdapter, ... (API CHANGE) BTAdapter removes itself from BTManager, hence needs to ensure BTManager is not yet destructed. This goes well along with our new JNI mapping, holding the shared_ptr reference in nativeInstance. --- java/jau/direct_bt/DBTManager.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'java/jau/direct_bt/DBTManager.java') diff --git a/java/jau/direct_bt/DBTManager.java b/java/jau/direct_bt/DBTManager.java index dfc332f9..243d52af 100644 --- a/java/jau/direct_bt/DBTManager.java +++ b/java/jau/direct_bt/DBTManager.java @@ -279,11 +279,11 @@ public class DBTManager implements BTManager } } - private native void initImpl() throws BTException; - private native void deleteImpl(long nativeInstance); + private native long ctorImpl() throws BTException; + private native void dtorImpl(long nativeInstance); private DBTManager() { - initImpl(); + nativeInstance = ctorImpl(); try { adapters.addAll(getAdapterListImpl()); } catch (final BTException be) { @@ -314,6 +314,14 @@ public class DBTManager implements BTManager a.close(); } adapters.clear(); - deleteImpl(nativeInstance); + + final long handle; + synchronized( this ) { + handle = nativeInstance; + nativeInstance = 0; + } + if( 0 != handle ) { + dtorImpl(handle); + } } } -- cgit v1.2.3