From 25d3e11305d04257e8d284d0786b5503e64fa4cb Mon Sep 17 00:00:00 2001 From: Steve Hawley Date: Fri, 21 Jan 2022 10:03:49 -0500 Subject: [PATCH] Clean up dispose pattern, remove sentence fragment --- .../ScanningAndDetecting3DObjects/README.md | 2 +- .../ScanningApp/3D_UI/BoundingBox.cs | 14 ++--------- .../ScanningApp/3D_UI/ObjectOrigin.cs | 6 +---- .../ScanningApp/3D_UI/ScannedObject.cs | 6 +---- .../ScanningApp/3D_UI/ScannedPointCloud.cs | 6 +---- .../ScanningApp/Controllers/Scan.cs | 16 ++++++++---- .../ScanningApp/Controllers/TestRun.cs | 25 ++++++++++++------- 7 files changed, 33 insertions(+), 42 deletions(-) diff --git a/dotnet/ios/ios12/ScanningAndDetecting3DObjects/README.md b/dotnet/ios/ios12/ScanningAndDetecting3DObjects/README.md index 9261a2d6..fd054b91 100644 --- a/dotnet/ios/ios12/ScanningAndDetecting3DObjects/README.md +++ b/dotnet/ios/ios12/ScanningAndDetecting3DObjects/README.md @@ -124,7 +124,7 @@ The Swift code uses five files to organize the behavior of the single class `Vie I occasionally refactored the Swift code where I thought it helped legibility or reduced repetition. Usually this was in the form of extracting a function. -One area where there is considerably variance from the Swift code is in the "Utility_Extensions" folder. Xamarin.iOS uses OpenTK vector and matrix classes for manipulating 3D objects and transforms. The OpenTK classes have different APIs and +One area where there is considerably variance from the Swift code is in the "Utility_Extensions" folder. Xamarin.iOS uses OpenTK vector and matrix classes for manipulating 3D objects and transforms. A matter of internal debate was the clearest form for porting Swift `guard` statements that assign a local variable if the passed-in variable is not `null`. The options are: diff --git a/dotnet/ios/ios12/ScanningAndDetecting3DObjects/ScanningApp/3D_UI/BoundingBox.cs b/dotnet/ios/ios12/ScanningAndDetecting3DObjects/ScanningApp/3D_UI/BoundingBox.cs index 43639568..255c7a1c 100644 --- a/dotnet/ios/ios12/ScanningAndDetecting3DObjects/ScanningApp/3D_UI/BoundingBox.cs +++ b/dotnet/ios/ios12/ScanningAndDetecting3DObjects/ScanningApp/3D_UI/BoundingBox.cs @@ -801,26 +801,16 @@ internal class BoundingBox : SCNNode // Dispose pattern for debugging purposes bool disposed = false; - internal bool IsDisposed - { - get - { - return disposed; - } - } + internal bool IsDisposed => disposed; override protected void Dispose (bool disposing) { if (!disposed) { disposed = true; - if (disposing) - { - base.Dispose (); - } - NSNotificationCenter.DefaultCenter.RemoveObserver (notificationObserverHandle); } + base.Dispose (disposing); } internal new void Dispose () diff --git a/dotnet/ios/ios12/ScanningAndDetecting3DObjects/ScanningApp/3D_UI/ObjectOrigin.cs b/dotnet/ios/ios12/ScanningAndDetecting3DObjects/ScanningApp/3D_UI/ObjectOrigin.cs index f81a5357..a3845610 100644 --- a/dotnet/ios/ios12/ScanningAndDetecting3DObjects/ScanningApp/3D_UI/ObjectOrigin.cs +++ b/dotnet/ios/ios12/ScanningAndDetecting3DObjects/ScanningApp/3D_UI/ObjectOrigin.cs @@ -515,13 +515,9 @@ internal class ObjectOrigin : SCNNode { disposed = true; - if (disposing) - { - base.Dispose (); - } - NSNotificationCenter.DefaultCenter.RemoveObserver (notificationObserverHandles [0]); NSNotificationCenter.DefaultCenter.RemoveObserver (notificationObserverHandles [1]); } + base.Dispose (disposing); } } diff --git a/dotnet/ios/ios12/ScanningAndDetecting3DObjects/ScanningApp/3D_UI/ScannedObject.cs b/dotnet/ios/ios12/ScanningAndDetecting3DObjects/ScanningApp/3D_UI/ScannedObject.cs index bb9bb4be..25db0ef4 100644 --- a/dotnet/ios/ios12/ScanningAndDetecting3DObjects/ScanningApp/3D_UI/ScannedObject.cs +++ b/dotnet/ios/ios12/ScanningAndDetecting3DObjects/ScanningApp/3D_UI/ScannedObject.cs @@ -341,16 +341,12 @@ internal class ScannedObject : SCNNode if (!disposed) { disposed = true; - if (disposing) - { - base.Dispose (); - } - NSNotificationCenter.DefaultCenter.RemoveObserver (notificationObserverHandle); Origin?.Dispose (); BoundingBox?.Dispose (); GhostBoundingBox?.Dispose (); } + base.Dispose (disposing); } #endregion diff --git a/dotnet/ios/ios12/ScanningAndDetecting3DObjects/ScanningApp/3D_UI/ScannedPointCloud.cs b/dotnet/ios/ios12/ScanningAndDetecting3DObjects/ScanningApp/3D_UI/ScannedPointCloud.cs index 33fbe91a..72a68a7e 100644 --- a/dotnet/ios/ios12/ScanningAndDetecting3DObjects/ScanningApp/3D_UI/ScannedPointCloud.cs +++ b/dotnet/ios/ios12/ScanningAndDetecting3DObjects/ScanningApp/3D_UI/ScannedPointCloud.cs @@ -142,15 +142,11 @@ internal class ScannedPointCloud : SCNNode if (!disposed) { disposed = true; - if (disposing) - { - base.Dispose (); - } - foreach (var notificationObserverHandle in notificationObservationHandles) { NSNotificationCenter.DefaultCenter.RemoveObserver (notificationObserverHandle); } } + base.Dispose (disposing); } } diff --git a/dotnet/ios/ios12/ScanningAndDetecting3DObjects/ScanningApp/Controllers/Scan.cs b/dotnet/ios/ios12/ScanningAndDetecting3DObjects/ScanningApp/Controllers/Scan.cs index df6fc024..aa10a751 100644 --- a/dotnet/ios/ios12/ScanningAndDetecting3DObjects/ScanningApp/Controllers/Scan.cs +++ b/dotnet/ios/ios12/ScanningAndDetecting3DObjects/ScanningApp/Controllers/Scan.cs @@ -152,6 +152,7 @@ Do you want to go back and adjust the bounding box of the scanned object?"""; sceneView.Scene.RootNode.AddChildNode (pointCloud); } + bool disposed; internal new void Dispose () { Dispose (true); @@ -160,14 +161,19 @@ Do you want to go back and adjust the bounding box of the scanned object?"""; protected override void Dispose (bool disposing) { - if (disposing) + if (!disposed) { - scannedObject?.RemoveFromParentNode (); - pointCloud?.RemoveFromParentNode (); + disposed = true; + if (disposing) + { + scannedObject?.RemoveFromParentNode (); + pointCloud?.RemoveFromParentNode (); - scannedObject?.Dispose (); - pointCloud?.Dispose (); + scannedObject?.Dispose (); + pointCloud?.Dispose (); + } } + base.Dispose (disposing); } void ApplicationStateChanged (NSNotification notification) diff --git a/dotnet/ios/ios12/ScanningAndDetecting3DObjects/ScanningApp/Controllers/TestRun.cs b/dotnet/ios/ios12/ScanningAndDetecting3DObjects/ScanningApp/Controllers/TestRun.cs index c7059235..e7351bb6 100644 --- a/dotnet/ios/ios12/ScanningAndDetecting3DObjects/ScanningApp/Controllers/TestRun.cs +++ b/dotnet/ios/ios12/ScanningAndDetecting3DObjects/ScanningApp/Controllers/TestRun.cs @@ -115,6 +115,8 @@ internal class TestRun : NSObject, IDisposable noDetectionTimer = null; } + bool disposed; + internal new void Dispose () { Dispose (true); @@ -123,18 +125,23 @@ internal class TestRun : NSObject, IDisposable protected override void Dispose (bool disposing) { - if (disposing) + if (!disposed) { - DetectedObject?.RemoveFromParentNode (); - - if (sceneView.Session.Configuration as ARWorldTrackingConfiguration is not null) + disposed = true; + if (disposing) { - // Make sure we switch back to an object scanning configuration & no longer - // try to detect the object. - var configuration = new ARObjectScanningConfiguration (); - configuration.PlaneDetection = ARPlaneDetection.Horizontal; - sceneView.Session.Run (configuration, ARSessionRunOptions.ResetTracking); + DetectedObject?.RemoveFromParentNode (); + + if (sceneView.Session.Configuration as ARWorldTrackingConfiguration is not null) + { + // Make sure we switch back to an object scanning configuration & no longer + // try to detect the object. + var configuration = new ARObjectScanningConfiguration (); + configuration.PlaneDetection = ARPlaneDetection.Horizontal; + sceneView.Session.Run (configuration, ARSessionRunOptions.ResetTracking); + } } + base.Dispose (disposing); } } }