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 /api/direct_bt/GATTService.hpp | |
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 'api/direct_bt/GATTService.hpp')
-rw-r--r-- | api/direct_bt/GATTService.hpp | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/api/direct_bt/GATTService.hpp b/api/direct_bt/GATTService.hpp index c02628fe..fd70e24a 100644 --- a/api/direct_bt/GATTService.hpp +++ b/api/direct_bt/GATTService.hpp @@ -62,10 +62,11 @@ namespace direct_bt { * which also may include its client config if available. */ class GATTService : public JavaUplink { - public: + private: /* Service's Device back-reference */ - std::shared_ptr<DBTDevice> device; + std::weak_ptr<DBTDevice> wbr_device; + public: const bool isPrimary; /** @@ -92,7 +93,7 @@ namespace direct_bt { GATTService(const std::shared_ptr<DBTDevice> &device, const bool isPrimary, const uint16_t startHandle, const uint16_t endHandle, std::shared_ptr<const uuid_t> type) - : device(device), isPrimary(isPrimary), startHandle(startHandle), endHandle(endHandle), type(type), characteristicList() { + : wbr_device(device), isPrimary(isPrimary), startHandle(startHandle), endHandle(endHandle), type(type), characteristicList() { characteristicList.reserve(10); } @@ -103,6 +104,8 @@ namespace direct_bt { return std::string(JAVA_DBT_PACKAGE "DBTGattService"); } + std::shared_ptr<DBTDevice> getDevice() const { return wbr_device.lock(); } + std::string toString() const; }; |