From b12bad7d0f834e8043660831b7dd95b7bff99cd3 Mon Sep 17 00:00:00 2001 From: Waters Date: Wed, 15 Jan 2025 14:43:36 -0500 Subject: [PATCH 1/2] made windows BLE device disposable to allow control of freeing resources --- .../Windows/BluetoothDevice.windows.cs | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/InTheHand.BluetoothLE/Platforms/Windows/BluetoothDevice.windows.cs b/InTheHand.BluetoothLE/Platforms/Windows/BluetoothDevice.windows.cs index f544e15..3fe538c 100644 --- a/InTheHand.BluetoothLE/Platforms/Windows/BluetoothDevice.windows.cs +++ b/InTheHand.BluetoothLE/Platforms/Windows/BluetoothDevice.windows.cs @@ -15,7 +15,7 @@ namespace InTheHand.Bluetooth { - partial class BluetoothDevice + partial class BluetoothDevice : IDisposable { internal BluetoothLEDevice NativeDevice; internal readonly ConcurrentDictionary NativeDisposeList = new ConcurrentDictionary(); @@ -40,7 +40,7 @@ internal BluetoothDevice(BluetoothLEDevice device) ~BluetoothDevice() { - DisposeAllNativeObjects(); + Dispose(disposing: false); } /// Adds a native (IDisposable) object to the dispose list @@ -249,5 +249,25 @@ void DoUnwatchAdvertisements() _advertisementWatcher.Stop(); } }*/ + + private void Dispose(bool disposing) + { + if (!disposedValue) + { + if (disposing) + { + // TODO: dispose managed state (managed objects) + } + + DisposeAllNativeObjects(); + disposedValue = true; + } + } + + public void Dispose() + { + Dispose(disposing: true); + GC.SuppressFinalize(this); + } } } From 29f408b811041e89e61205d3981b46bff7bb4acc Mon Sep 17 00:00:00 2001 From: Waters Date: Mon, 3 Mar 2025 10:32:13 -0500 Subject: [PATCH 2/2] added missing `_disposed` field --- .../Platforms/Windows/BluetoothDevice.windows.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/InTheHand.BluetoothLE/Platforms/Windows/BluetoothDevice.windows.cs b/InTheHand.BluetoothLE/Platforms/Windows/BluetoothDevice.windows.cs index 3fe538c..9e9f9c1 100644 --- a/InTheHand.BluetoothLE/Platforms/Windows/BluetoothDevice.windows.cs +++ b/InTheHand.BluetoothLE/Platforms/Windows/BluetoothDevice.windows.cs @@ -22,6 +22,7 @@ partial class BluetoothDevice : IDisposable private string _cachedId; private string _cachedName; internal ulong LastKnownAddress; + private bool _disposed; internal BluetoothDevice(BluetoothLEDevice device) { @@ -252,7 +253,7 @@ void DoUnwatchAdvertisements() private void Dispose(bool disposing) { - if (!disposedValue) + if (!_disposed) { if (disposing) { @@ -260,7 +261,7 @@ private void Dispose(bool disposing) } DisposeAllNativeObjects(); - disposedValue = true; + _disposed = true; } }