Using Windows 10 and Windows 11 I noticed that we have a memory leak when adding and delete a BLE device. To make things simpler, I used an example from the MBS xojo plugins set (BluetoothLE Device). This example find a BLE device by required name and then find and fill the device with one custam service and characteristics of this service. I only added 2 buttons: to add new device and delete all devices. Devices are contained in the Devices() of the MainWindow.
I noticed, when I delete a device, it’s still contained in memory(1 WindowsBluetoothLEDevice, 1 WindowsGattDeviceService, 2 WindowsGattCharacteristic in our case). When I add a new one this set of objects is increased by one.
Code of delete button:
for each device as WindowsBluetoothLEDevice in Self.Devices
for each service as WindowsGattDeviceService in device.GattServices
for each characteristic as WindowsGattCharacteristic in service.Characteristics
characteristic.Destructor
next
service.Session.Close
service.Characteristics.RemoveAll
service.Close
next
device.GattServices.RemoveAll
device.Pairing.Close
device.Close
next
Devices.RemoveAll
I tried to use everything available for destruction, but nothing helps. Maybe I missed something?
Interesting details: BLE device is removed from memory if you do not use GetGattServicesAsync and RequestAccessAsync. The same thing for services - they leave memory if you don’t call GetCharacteristicsAsync or any.