Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deinit UARTService CharacteristicBuffers on disconnect #193

Merged
merged 1 commit into from
Mar 4, 2024

Conversation

dhalbert
Copy link
Collaborator

@dhalbert dhalbert commented Feb 29, 2024

CharacteristicBuffer must be deinit'd after use, to prevent a storage leak. Do this for UARTService, and provide a mechanism to invoke deinit() on disconnect().

test to detect storage leak:

import os, gc
import time
import board
from adafruit_ble import BLERadio
from adafruit_ble.services.nordic import UARTService
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement

ble = BLERadio()
uart = UARTService()

while True:
    print("Scanning ...")
    for adv in ble.start_scan(ProvideServicesAdvertisement, timeout=1):
        if UARTService not in adv.services:
            continue
        count = 1
        while True:
            try:
                conn = ble.connect(adv)
                svc = conn[UARTService]
                print(f"{count} Free memory: {gc.mem_free()}, Allocated memory: {gc.mem_alloc()}")
                conn.disconnect()
                conn = None
                svc = None
                gc.collect()
            except Exception as e:
                print("Error: ", e)
            count += 1

@dhalbert dhalbert requested a review from a team February 29, 2024 21:35
@dhalbert
Copy link
Collaborator Author

dhalbert commented Mar 4, 2024

Could someone review this? Thanks.

Copy link
Member

@tekktrik tekktrik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@dhalbert dhalbert merged commit 28349ae into adafruit:main Mar 4, 2024
1 check passed
@dhalbert dhalbert deleted the deinit-characteristic-buffers branch March 4, 2024 21:29
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Mar 5, 2024
Updating https://github.com/adafruit/Adafruit_CircuitPython_FONA to 3.0.4 from 3.0.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_FONA#32 from justmobilize/update-legacy-set-socket-examples

Updating https://github.com/adafruit/Adafruit_CircuitPython_PyPortal to 6.3.3 from 6.3.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_PyPortal#133 from justmobilize/fix-get-radio-ssl-context
  > Merge pull request adafruit/Adafruit_CircuitPython_PyPortal#132 from justmobilize/update-legacy-set-socket-examples

Updating https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k to 5.0.7 from 5.0.6:
  > Merge pull request adafruit/Adafruit_CircuitPython_Wiznet5k#147 from justmobilize/fix-get-radio-ssl-context
  > Merge pull request adafruit/Adafruit_CircuitPython_Wiznet5k#146 from justmobilize/update-legacy-set-socket-examples

Updating https://github.com/adafruit/Adafruit_CircuitPython_AWS_IOT to 2.0.18 from 2.0.17:
  > Merge pull request adafruit/Adafruit_CircuitPython_AWS_IOT#26 from justmobilize/fix-get-radio-ssl-context
  > Merge pull request adafruit/Adafruit_CircuitPython_AWS_IOT#25 from justmobilize/update-legacy-set-socket-examples
  > Merge pull request adafruit/Adafruit_CircuitPython_AWS_IOT#24 from justmobilize/fix-readme-requirements

Updating https://github.com/adafruit/Adafruit_CircuitPython_GC_IOT_Core to 3.2.11 from 3.2.10:
  > Merge pull request adafruit/Adafruit_CircuitPython_GC_IOT_Core#29 from justmobilize/fix-get-radio-ssl-context
  > Merge pull request adafruit/Adafruit_CircuitPython_GC_IOT_Core#28 from justmobilize/update-legacy-set-socket-examples
  > Merge pull request adafruit/Adafruit_CircuitPython_GC_IOT_Core#27 from justmobilize/fix-readme-requirements

Updating https://github.com/adafruit/Adafruit_CircuitPython_AdafruitIO to 5.8.2 from 5.8.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_AdafruitIO#118 from justmobilize/fix-get-radio-ssl-context
  > Merge pull request adafruit/Adafruit_CircuitPython_AdafruitIO#117 from justmobilize/update-legacy-set-socket-examples
  > Merge pull request adafruit/Adafruit_CircuitPython_AdafruitIO#116 from justmobilize/fix-readme-requirements

Updating https://github.com/adafruit/Adafruit_CircuitPython_AzureIoT to 2.5.18 from 2.5.17:
  > Merge pull request adafruit/Adafruit_CircuitPython_AzureIoT#64 from justmobilize/fix-readme-requirements
  > Merge pull request adafruit/Adafruit_CircuitPython_AzureIoT#63 from justmobilize/fix-ssl-and-set-socket

Updating https://github.com/adafruit/Adafruit_CircuitPython_BLE to 10.0.6 from 10.0.5:
  > Merge pull request adafruit/Adafruit_CircuitPython_BLE#193 from dhalbert/deinit-characteristic-buffers

Updating https://github.com/adafruit/Adafruit_CircuitPython_Dash_Dis to 2.1.2 from 2.1.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_Dash_Dis#18 from justmobilize/fix-get-radio-ssl-context
  > Merge pull request adafruit/Adafruit_CircuitPython_Dash_Dis#17 from justmobilize/update-legacy-set-socket-examples

Updating https://github.com/adafruit/Adafruit_CircuitPython_JSON_Stream to 0.8.3 from 0.8.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_JSON_Stream#3 from bablokb/main

Updating https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT to 7.6.3 from 7.6.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#208 from justmobilize/change-free-to-close
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#205 from LelandSindt/main
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#206 from justmobilize/fix-get-radio-ssl-context

Updating https://github.com/adafruit/Adafruit_CircuitPython_OAuth2 to 1.0.17 from 1.0.16:
  > Merge pull request adafruit/Adafruit_CircuitPython_OAuth2#12 from justmobilize/fix-get-radio-ssl-context
  > Merge pull request adafruit/Adafruit_CircuitPython_OAuth2#11 from justmobilize/update-legacy-set-socket-examples
  > Merge pull request adafruit/Adafruit_CircuitPython_OAuth2#10 from justmobilize/fix-readme-requirements

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Updated download stats for the libraries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memory leak after repeated BLE reconnection and UARTService activation
2 participants