Skip to content

Commit 26ad786

Browse files
author
David Kline
authored
Merge pull request #3488 from davidkline-ms/disposePattern
update dispose pattern implementation, spatial mesh observer now uses dispose
2 parents ba4a7da + 55e7330 commit 26ad786

File tree

2 files changed

+74
-26
lines changed

2 files changed

+74
-26
lines changed

Assets/MixedRealityToolkit.Providers/WindowsMixedReality/WindowsMixedRealitySpatialMeshObserver.cs

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,24 @@ private void ReadProfile()
6969

7070
#region IMixedRealityToolkit implementation
7171

72+
#if UNITY_WSA
73+
7274
/// <inheritdoc />
7375
public override void Initialize()
7476
{
7577
// Only initialize if the Spatial Awareness system has been enabled in the configuration profile.
7678
if (!MixedRealityToolkit.Instance.ActiveProfile.IsSpatialAwarenessSystemEnabled) { return; }
7779

78-
#if UNITY_WSA
7980
CreateObserver();
8081

8182
// Apply the initial observer volume settings.
8283
ConfigureObserverVolume();
83-
#endif // UNITY_WSA
8484
}
8585

8686
/// <inheritdoc />
8787
public override void Reset()
8888
{
89-
#if UNITY_WSA
9089
CleanupObserver();
91-
#endif // UNITY_WSA
9290
Initialize();
9391
}
9492

@@ -101,9 +99,7 @@ public override void Enable()
10199
/// <inheritdoc />
102100
public override void Update()
103101
{
104-
#if UNITY_WSA
105102
UpdateObserver();
106-
#endif // UNITY_WSA
107103
}
108104

109105
/// <inheritdoc />
@@ -115,11 +111,11 @@ public override void Disable()
115111
/// <inheritdoc />
116112
public override void Destroy()
117113
{
118-
#if UNITY_WSA
119114
CleanupObserver();
120-
#endif // UNITY_WSA
121115
}
122116

117+
#endif // UNITY_WSA
118+
123119
#endregion IMixedRealityToolkit implementation
124120

125121
#region IMixedRealitySpatialAwarenessObserver implementation
@@ -140,6 +136,28 @@ public override void Destroy()
140136
private IMixedRealitySpatialAwarenessSystem SpatialAwarenessSystem => spatialAwarenessSystem ?? (spatialAwarenessSystem = MixedRealityToolkit.SpatialAwarenessSystem);
141137

142138
#if UNITY_WSA
139+
/// <inheritdoc />
140+
protected override void Dispose(bool disposing)
141+
{
142+
if (disposed) { return; }
143+
144+
base.Dispose(disposing);
145+
146+
if (IsRunning)
147+
{
148+
Suspend();
149+
}
150+
151+
if (disposing)
152+
{
153+
CleanupObservedObjects();
154+
}
155+
156+
DisposeObserver();
157+
158+
disposed = true;
159+
}
160+
143161
/// <summary>
144162
/// The surface observer providing the spatial data.
145163
/// </summary>
@@ -292,24 +310,23 @@ private void CreateObserver()
292310
/// </summary>
293311
private void CleanupObserver()
294312
{
295-
if (observer != null)
296-
{
297-
if (IsRunning)
298-
{
299-
Suspend();
300-
}
301-
observer.Dispose();
302-
observer = null;
303-
}
313+
// Destroys all observed objects and the observer.
314+
Dispose(true);
315+
}
304316

317+
/// <summary>
318+
/// Cleans up the objects created during observation.
319+
/// </summary>
320+
private void CleanupObservedObjects()
321+
{
305322
if (Application.isPlaying)
306323
{
307324
// Cleanup the scene objects are managing
308325
if (observedObjectParent != null)
309326
{
310327
observedObjectParent.transform.DetachChildren();
311328
}
312-
329+
313330
foreach (SpatialAwarenessMeshObject meshObject in meshes.Values)
314331
{
315332
if (meshObject != null)
@@ -338,6 +355,18 @@ private void CleanupObserver()
338355
}
339356
}
340357

358+
/// <summary>
359+
/// Implements proper cleanup of the SurfaceObserver.
360+
/// </summary>
361+
private void DisposeObserver()
362+
{
363+
if (observer != null)
364+
{
365+
observer.Dispose();
366+
observer = null;
367+
}
368+
}
369+
341370
/// <summary>
342371
/// Requests updates from the surface observer.
343372
/// </summary>

Assets/MixedRealityToolkit/Services/BaseService.cs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,42 @@ public virtual void Destroy() { }
4040

4141
#region IDisposable Implementation
4242

43-
private bool disposed;
44-
43+
/// <summary>
44+
/// Value indicating if the object has completed disposal.
45+
/// </summary>
46+
/// <remarks>
47+
/// Set by derived classes to indicate that disposal has been completed.
48+
/// </remarks>
49+
protected bool disposed = false;
50+
51+
/// <summary>
52+
/// Finalizer
53+
/// </summary>
4554
~BaseService()
4655
{
47-
OnDispose(true);
56+
Dispose();
4857
}
4958

59+
/// <summary>
60+
/// Cleanup resources used by this object.
61+
/// </summary>
5062
public void Dispose()
5163
{
52-
if (disposed) { return; }
53-
disposed = true;
64+
// Clean up our resources (managed and unmanaged resources)
65+
Dispose(true);
66+
67+
// Suppress finalization as the the finalizer also calls our cleanup code.
5468
GC.SuppressFinalize(this);
55-
OnDispose(false);
5669
}
5770

58-
protected virtual void OnDispose(bool finalizing) { }
59-
71+
/// <summary>
72+
/// Cleanup resources used by the object
73+
/// </summary>
74+
/// <param name="disposing">Are we fully disposing the object?
75+
/// True will release all managed resources, unmanaged resources are always released.
76+
/// </param>
77+
protected virtual void Dispose(bool disposing) { }
78+
6079
#endregion IDisposable Implementation
6180
}
6281
}

0 commit comments

Comments
 (0)