Skip to content

Commit 28349ae

Browse files
authored
Merge pull request #193 from dhalbert/deinit-characteristic-buffers
Deinit UARTService CharacteristicBuffers on disconnect
2 parents 744933f + 9c010cd commit 28349ae

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

adafruit_ble/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ def pair(self, *, bond: bool = True) -> None:
145145
def disconnect(self) -> None:
146146
"""Disconnect from peer."""
147147
self._bleio_connection.disconnect()
148+
# Clean up any services that need explicit cleanup.
149+
for service in self._constructed_services.values():
150+
service.deinit()
148151

149152

150153
class BLERadio:

adafruit_ble/characteristics/stream.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ def __init__(
6969
uuid=uuid, properties=properties, read_perm=read_perm, write_perm=write_perm
7070
)
7171

72-
def bind(self, service: Service) -> Union[_bleio.Characteristic, BoundWriteStream]:
72+
def bind(
73+
self, service: Service
74+
) -> Union[_bleio.CharacteristicBuffer, BoundWriteStream]:
7375
"""Binds the characteristic to the given Service."""
7476
bound_characteristic = super().bind(service)
7577
# If we're given a remote service then we're the client and need to buffer in.

adafruit_ble/services/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ def __init__(
8282
else:
8383
getattr(self, class_attr)
8484

85+
def deinit(self):
86+
"""Override this method to do any explicit cleanup necessary on connection close."""
87+
8588
@property
8689
def remote(self) -> bool:
8790
"""True if the service is provided by a peer and accessed remotely."""

adafruit_ble/services/nordic.py

+8
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ def __init__(self, service: Optional[_bleio.Service] = None) -> None:
6262
self._tx = self._server_rx
6363
self._rx = self._server_tx
6464

65+
def deinit(self):
66+
"""The characteristic buffers must be deinitialized when no longer needed.
67+
Otherwise they will leak storage.
68+
"""
69+
for obj in (self._tx, self._rx):
70+
if hasattr(obj, "deinit"):
71+
obj.deinit()
72+
6573
def read(self, nbytes: Optional[int] = None) -> Optional[bytes]:
6674
"""
6775
Read characters. If ``nbytes`` is specified then read at most that many bytes.

0 commit comments

Comments
 (0)