Skip to content

Commit 55e7330

Browse files
author
David Kline (ANALOG)
committed
update spatial mesh obsever to use dispose pattern for cleanup
1 parent 1f33c66 commit 55e7330

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
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>

0 commit comments

Comments
 (0)