Skip to content

Commit fdc69fd

Browse files
committed
Implemented recommended dispose pattern for DeviceProviders
1 parent 56d77de commit fdc69fd

File tree

15 files changed

+103
-32
lines changed

15 files changed

+103
-32
lines changed

RGB.NET.Core/Devices/AbstractRGBDeviceProvider.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public abstract class AbstractRGBDeviceProvider : IRGBDeviceProvider
1212
{
1313
#region Properties & Fields
1414

15+
private bool _isDisposed = false;
16+
1517
private readonly double _defaultUpdateRateHardLimit;
1618

1719
/// <inheritdoc />
@@ -60,13 +62,17 @@ protected AbstractRGBDeviceProvider(double defaultUpdateRateHardLimit = 0)
6062
this._defaultUpdateRateHardLimit = defaultUpdateRateHardLimit;
6163
}
6264

65+
~AbstractRGBDeviceProvider() => Dispose(false);
66+
6367
#endregion
6468

6569
#region Methods
6670

6771
/// <inheritdoc />
6872
public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool throwExceptions = false)
6973
{
74+
if (_isDisposed) throw new ObjectDisposedException(GetType().FullName);
75+
7076
ThrowsExceptions = throwExceptions;
7177

7278
try
@@ -108,6 +114,8 @@ public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool throwE
108114
/// <returns>The filtered collection of loaded devices.</returns>
109115
protected virtual IEnumerable<IRGBDevice> GetLoadedDevices(RGBDeviceType loadFilter)
110116
{
117+
if (_isDisposed) throw new ObjectDisposedException(GetType().FullName);
118+
111119
List<IRGBDevice> devices = new();
112120
foreach (IRGBDevice device in LoadDevices())
113121
{
@@ -152,6 +160,8 @@ protected virtual IEnumerable<IRGBDevice> GetLoadedDevices(RGBDeviceType loadFil
152160
/// <returns>The update trigger mapped to the specified id.</returns>
153161
protected virtual IDeviceUpdateTrigger GetUpdateTrigger(int id = -1, double? updateRateHardLimit = null)
154162
{
163+
if (_isDisposed) throw new ObjectDisposedException(GetType().FullName);
164+
155165
if (!UpdateTriggerMapping.TryGetValue(id, out IDeviceUpdateTrigger? updaeTrigger))
156166
UpdateTriggerMapping[id] = (updaeTrigger = CreateUpdateTrigger(id, updateRateHardLimit ?? _defaultUpdateRateHardLimit));
157167

@@ -171,6 +181,8 @@ protected virtual IDeviceUpdateTrigger GetUpdateTrigger(int id = -1, double? upd
171181
/// </summary>
172182
protected virtual void Reset()
173183
{
184+
if (_isDisposed) throw new ObjectDisposedException(GetType().FullName);
185+
174186
foreach (IDeviceUpdateTrigger updateTrigger in UpdateTriggerMapping.Values)
175187
updateTrigger.Dispose();
176188

@@ -192,6 +204,8 @@ protected virtual void Reset()
192204
/// <returns><c>true</c> if the device was added successfully; otherwise <c>false</c>.</returns>
193205
protected virtual bool AddDevice(IRGBDevice device)
194206
{
207+
if (_isDisposed) throw new ObjectDisposedException(GetType().FullName);
208+
195209
if (InternalDevices.Contains(device)) return false;
196210

197211
InternalDevices.Add(device);
@@ -207,6 +221,8 @@ protected virtual bool AddDevice(IRGBDevice device)
207221
/// <returns><c>true</c> if the device was removed successfully; otherwise <c>false</c>.</returns>
208222
protected virtual bool RemoveDevice(IRGBDevice device)
209223
{
224+
if (_isDisposed) throw new ObjectDisposedException(GetType().FullName);
225+
210226
if (!InternalDevices.Remove(device)) return false;
211227

212228
try { OnDevicesChanged(DevicesChangedEventArgs.CreateDevicesRemovedArgs(device)); } catch { /* we don't want to throw due to bad event handlers */ }
@@ -241,12 +257,26 @@ public virtual void Throw(Exception ex, bool isCritical = false)
241257
protected virtual void OnDevicesChanged(DevicesChangedEventArgs args) => DevicesChanged?.Invoke(this, args);
242258

243259
/// <inheritdoc />
244-
public virtual void Dispose()
260+
public void Dispose()
245261
{
246-
Reset();
262+
if (_isDisposed) return;
263+
264+
try
265+
{
266+
Dispose(true);
267+
}
268+
catch { /* don't throw in dispose! */ }
247269

248270
GC.SuppressFinalize(this);
271+
272+
_isDisposed = true;
249273
}
250274

275+
/// <summary>
276+
/// Disposes the object and frees all resources.
277+
/// </summary>
278+
/// <param name="disposing"><c>true</c> if explicitely called through the Dispose-Method, <c>false</c> if called by the destructor.</param>
279+
protected virtual void Dispose(bool disposing) => Reset();
280+
251281
#endregion
252282
}

RGB.NET.Devices.Asus/AsusDeviceProvider.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,16 @@ protected override IEnumerable<IRGBDevice> LoadDevices()
8080
}
8181

8282
/// <inheritdoc />
83-
public override void Dispose()
83+
protected override void Dispose(bool disposing)
8484
{
85-
base.Dispose();
85+
base.Dispose(disposing);
8686

8787
try { _sdk?.ReleaseControl(0); }
8888
catch { /* at least we tried */ }
8989

9090
_devices = null;
9191
_sdk = null;
92+
_instance = null;
9293
}
9394

9495
#endregion

RGB.NET.Devices.CoolerMaster/CoolerMasterDeviceProvider.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,14 @@ protected override IEnumerable<IRGBDevice> LoadDevices()
9494
}
9595

9696
/// <inheritdoc />
97-
public override void Dispose()
97+
protected override void Dispose(bool disposing)
9898
{
99-
base.Dispose();
99+
base.Dispose(disposing);
100100

101101
try { _CoolerMasterSDK.Reload(); }
102102
catch { /* Unlucky.. */ }
103+
104+
_instance = null;
103105
}
104106

105107
#endregion

RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,14 @@ private IEnumerable<ICorsairRGBDevice> LoadCorsairDevices()
300300
}
301301

302302
/// <inheritdoc />
303-
public override void Dispose()
303+
protected override void Dispose(bool disposing)
304304
{
305-
base.Dispose();
305+
base.Dispose(disposing);
306306

307307
try { _CUESDK.CorsairDisconnect(); } catch { /* at least we tried */ }
308308
try { _CUESDK.UnloadCUESDK(); } catch { /* at least we tried */ }
309+
310+
_instance = null;
309311
}
310312

311313
#endregion

RGB.NET.Devices.DMX/DMXDeviceProvider.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,13 @@ protected override IDeviceUpdateTrigger CreateUpdateTrigger(int id, double updat
8686
return updateTrigger;
8787
}
8888

89+
/// <inheritdoc />
90+
protected override void Dispose(bool disposing)
91+
{
92+
base.Dispose(disposing);
93+
94+
_instance = null;
95+
}
96+
8997
#endregion
9098
}

RGB.NET.Devices.Debug/DebugDeviceProvider.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,13 @@ protected override IEnumerable<IRGBDevice> LoadDevices()
6666
}
6767

6868
/// <inheritdoc />
69-
public override void Dispose()
69+
protected override void Dispose(bool disposing)
7070
{
71-
base.Dispose();
71+
base.Dispose(disposing);
7272

7373
_fakeDeviceDefinitions.Clear();
74+
75+
_instance = null;
7476
}
7577

7678
#endregion

RGB.NET.Devices.Logitech/LogitechDeviceProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,17 +254,17 @@ protected override IEnumerable<IRGBDevice> LoadDevices()
254254
}
255255

256256
/// <inheritdoc />
257-
public override void Dispose()
257+
protected override void Dispose(bool disposing)
258258
{
259-
base.Dispose();
259+
base.Dispose(disposing);
260260

261261
try { _LogitechGSDK.LogiLedRestoreLighting(); }
262262
catch { /* at least we tried */ }
263263

264264
try { _LogitechGSDK.UnloadLogitechGSDK(); }
265265
catch { /* at least we tried */ }
266266

267-
GC.SuppressFinalize(this);
267+
_instance = null;
268268
}
269269

270270
#endregion

RGB.NET.Devices.Msi/MsiDeviceProvider.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,14 @@ protected override IEnumerable<IRGBDevice> LoadDevices()
9898
private void ThrowMsiError(int errorCode, bool isCritical = false) => Throw(new MysticLightException(errorCode, _MsiSDK.GetErrorMessage(errorCode)), isCritical);
9999

100100
/// <inheritdoc />
101-
public override void Dispose()
101+
protected override void Dispose(bool disposing)
102102
{
103-
base.Dispose();
103+
base.Dispose(disposing);
104104

105105
try { _MsiSDK.UnloadMsiSDK(); }
106106
catch { /* at least we tried */ }
107-
108-
GC.SuppressFinalize(this);
107+
108+
_instance = null;
109109
}
110110

111111
#endregion

RGB.NET.Devices.Novation/NovationDeviceProvider.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,13 @@ protected override IEnumerable<IRGBDevice> LoadDevices()
6767
}
6868
}
6969

70+
/// <inheritdoc />
71+
protected override void Dispose(bool disposing)
72+
{
73+
base.Dispose(disposing);
74+
75+
_instance = null;
76+
}
77+
7078
#endregion
7179
}

RGB.NET.Devices.OpenRGB/OpenRGBDeviceProvider.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,22 @@ protected override IEnumerable<IRGBDevice> LoadDevices()
107107
continue;
108108
}
109109

110-
if (device.Zones.Length == 0)
110+
if (device.Zones.Length == 0)
111111
continue;
112-
if (device.Zones.All(z => z.LedCount == 0))
112+
if (device.Zones.All(z => z.LedCount == 0))
113113
continue;
114114

115115
OpenRGBUpdateQueue updateQueue = new(GetUpdateTrigger(), i, openRgb, device);
116-
116+
117117
bool anyZoneHasSegments = device.Zones.Any(z => z.Segments.Length > 0);
118-
bool splitDeviceByZones = anyZoneHasSegments || PerZoneDeviceFlag.HasFlag(Helper.GetRgbNetDeviceType(device.Type));
118+
bool splitDeviceByZones = anyZoneHasSegments || PerZoneDeviceFlag.HasFlag(Helper.GetRgbNetDeviceType(device.Type));
119119

120120
if (!splitDeviceByZones)
121121
{
122122
yield return new OpenRGBGenericDevice(new OpenRGBDeviceInfo(device), updateQueue);
123123
continue;
124124
}
125-
125+
126126
int totalLedCount = 0;
127127

128128
foreach (Zone zone in device.Zones)
@@ -149,9 +149,9 @@ protected override IEnumerable<IRGBDevice> LoadDevices()
149149
}
150150

151151
/// <inheritdoc />
152-
public override void Dispose()
152+
protected override void Dispose(bool disposing)
153153
{
154-
base.Dispose();
154+
base.Dispose(disposing);
155155

156156
foreach (OpenRgbClient client in _clients)
157157
{
@@ -161,6 +161,8 @@ public override void Dispose()
161161

162162
_clients.Clear();
163163
DeviceDefinitions.Clear();
164+
165+
_instance = null;
164166
}
165167

166168
#endregion

0 commit comments

Comments
 (0)