diff options
author | Sven Gothel <[email protected]> | 2020-06-29 04:15:47 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-06-29 04:15:47 +0200 |
commit | dfdfd883f52e8d31c005d0f8ae42bbe6dd60c2b8 (patch) | |
tree | 14c599a06909fca30ceaf5de8946faec492e8c90 /java/jni | |
parent | e456a087c2e877df949f2dbd7fa95c25fe80a4ee (diff) |
Resolve circular references (p1): C++ GATTHandler, GATTService, pp are not owner of their resepctive backreference
GATTHandler, GATTService, pp are not owner of their resepctive backreference,
hence use std::weak_ptr for backreferences in general and validate on usage (nullptr, if destructed).
No DBTDevice has been ever destructed after using GATTHandler and discovering all GATT services.
In contrast to Java, C++ has no magic GC and hence shared_ptr use_count gets only increased
when emplying circular backreferences - none gets destructed.
Current ownership relationship is:
DBTAdapter -> DBTDevice -> GATTHandler -> GATTService ...
each contains a backreference, now using a weak_ptr.
Result is that depending on the use-case, DBTDevice instances are destructed:
- Using device->remove(): Immediately
- No explicit device->remove(): Adapter keeps sharedDevices, destruction occurs at end.
Diffstat (limited to 'java/jni')
-rw-r--r-- | java/jni/direct_bt/DBTDevice.cxx | 8 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTGattCharacteristic.cxx | 8 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTGattService.cxx | 8 |
3 files changed, 18 insertions, 6 deletions
diff --git a/java/jni/direct_bt/DBTDevice.cxx b/java/jni/direct_bt/DBTDevice.cxx index 0a75b77c..20b6060b 100644 --- a/java/jni/direct_bt/DBTDevice.cxx +++ b/java/jni/direct_bt/DBTDevice.cxx @@ -355,8 +355,12 @@ jobject Java_direct_1bt_tinyb_DBTDevice_getServicesImpl(JNIEnv *env, jobject obj std::function<jobject(JNIEnv*, jclass, jmethodID, GATTService*)> ctor_service = [](JNIEnv *env, jclass clazz, jmethodID clazz_ctor, GATTService *service)->jobject { // prepare adapter ctor - JavaGlobalObj::check(service->device->getJavaObject(), E_FILE_LINE); - jobject jdevice = JavaGlobalObj::GetObject(service->device->getJavaObject()); + std::shared_ptr<DBTDevice> device = service->getDevice(); + if( nullptr == device ) { + throw IllegalStateException("Service's DBTDevice destructed: "+service->toString(), E_FILE_LINE); + } + JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + jobject jdevice = JavaGlobalObj::GetObject(device->getJavaObject()); const jboolean isPrimary = service->isPrimary; const jstring uuid = from_string_to_jstring(env, directBTJNISettings.getUnifyUUID128Bit() ? service->type->toUUID128String() : diff --git a/java/jni/direct_bt/DBTGattCharacteristic.cxx b/java/jni/direct_bt/DBTGattCharacteristic.cxx index 38a55358..fb966a93 100644 --- a/java/jni/direct_bt/DBTGattCharacteristic.cxx +++ b/java/jni/direct_bt/DBTGattCharacteristic.cxx @@ -76,8 +76,12 @@ jobject Java_direct_1bt_tinyb_DBTGattCharacteristic_getDescriptorsImpl(JNIEnv *e std::function<jobject(JNIEnv*, jclass, jmethodID, GATTDescriptor *)> ctor_desc = [](JNIEnv *env, jclass clazz, jmethodID clazz_ctor, GATTDescriptor *descriptor)->jobject { // prepare adapter ctor - JavaGlobalObj::check(descriptor->characteristic->getJavaObject(), E_FILE_LINE); - jobject jcharacteristic = JavaGlobalObj::GetObject(descriptor->characteristic->getJavaObject()); + std::shared_ptr<GATTCharacteristic> characteristic = descriptor->getCharacteristic(); + if( nullptr == characteristic ) { + throw IllegalStateException("Descriptor's GATTCharacteristic destructed: "+descriptor->toString(), E_FILE_LINE); + } + JavaGlobalObj::check(characteristic->getJavaObject(), E_FILE_LINE); + jobject jcharacteristic = JavaGlobalObj::GetObject(characteristic->getJavaObject()); const jstring uuid = from_string_to_jstring(env, directBTJNISettings.getUnifyUUID128Bit() ? descriptor->type->toUUID128String() : diff --git a/java/jni/direct_bt/DBTGattService.cxx b/java/jni/direct_bt/DBTGattService.cxx index 4ded7658..f00f9e2f 100644 --- a/java/jni/direct_bt/DBTGattService.cxx +++ b/java/jni/direct_bt/DBTGattService.cxx @@ -76,8 +76,12 @@ jobject Java_direct_1bt_tinyb_DBTGattService_getCharacteristicsImpl(JNIEnv *env, std::function<jobject(JNIEnv*, jclass, jmethodID, GATTCharacteristic *)> ctor_char = [](JNIEnv *env, jclass clazz, jmethodID clazz_ctor, GATTCharacteristic *characteristic)->jobject { // prepare adapter ctor - JavaGlobalObj::check(characteristic->service->getJavaObject(), E_FILE_LINE); - jobject jservice = JavaGlobalObj::GetObject(characteristic->service->getJavaObject()); + std::shared_ptr<GATTService> service = characteristic->getService(); + if( nullptr == service ) { + throw IllegalStateException("Characteristic's GATTService destructed: "+characteristic->toString(), E_FILE_LINE); + } + JavaGlobalObj::check(service->getJavaObject(), E_FILE_LINE); + jobject jservice = JavaGlobalObj::GetObject(service->getJavaObject()); std::vector<std::unique_ptr<std::string>> props = GATTCharacteristic::getPropertiesStringList(characteristic->properties); unsigned int props_size = props.size(); |