From 3f18ad93267303dd22555eef00dd4950b4899a2f Mon Sep 17 00:00:00 2001 From: Andrew B Date: Wed, 30 Sep 2020 20:32:41 +0300 Subject: [PATCH] Minor improvements in releasing of Body Tracking resources --- .../Assets/Scripts/SkeletonProvider.cs | 4 +- K4AdotNet/BodyTracking/Tracker.cs | 33 +++- K4AdotNet/K4AdotNet.xml | 147 +----------------- 3 files changed, 28 insertions(+), 156 deletions(-) diff --git a/K4AdotNet.Samples.Unity/Assets/Scripts/SkeletonProvider.cs b/K4AdotNet.Samples.Unity/Assets/Scripts/SkeletonProvider.cs index d4f3034..db05bc9 100644 --- a/K4AdotNet.Samples.Unity/Assets/Scripts/SkeletonProvider.cs +++ b/K4AdotNet.Samples.Unity/Assets/Scripts/SkeletonProvider.cs @@ -64,11 +64,11 @@ private IEnumerator Start() private void OnDestroy() { + IsAvailable = false; + var captureManager = FindObjectOfType(); if (captureManager != null) captureManager.CaptureReady -= CaptureManager_CaptureReady; _tracker?.Dispose(); - - IsAvailable = false; } private void CaptureManager_CaptureReady(object sender, CaptureEventArgs e) diff --git a/K4AdotNet/BodyTracking/Tracker.cs b/K4AdotNet/BodyTracking/Tracker.cs index 3dc3f4c..7c4b1b9 100644 --- a/K4AdotNet/BodyTracking/Tracker.cs +++ b/K4AdotNet/BodyTracking/Tracker.cs @@ -16,6 +16,7 @@ public sealed class Tracker : IDisposablePlus { private readonly NativeHandles.HandleWrapper handle; // this class is an wrapper around this handle private volatile int queueSize; // captures in queue + private volatile bool isDisposed; private float temporalSmoothingFactor = DefaultSmoothingFactor; private readonly object temporalSmoothingFactorSync = new object(); @@ -68,11 +69,16 @@ private void Handle_Disposed(object sender, EventArgs e) /// /// public void Dispose() - => handle.Dispose(); + { + isDisposed = true; + if (!handle.IsDisposed) + Shutdown(); + handle.Dispose(); + } /// Gets a value indicating whether the object has been disposed of. /// - public bool IsDisposed => handle.IsDisposed; + public bool IsDisposed => isDisposed; /// Raised on object disposing (only once). /// @@ -89,11 +95,9 @@ public void Dispose() /// This function may be called while another thread is blocking in or . /// Calling this function while another thread is in that function will result in that function raising an exception. /// + /// Object was disposed. public void Shutdown() - { - NativeApi.TrackerShutdown(handle.Value); - queueSize = 0; - } + => NativeApi.TrackerShutdown(handle.ValueNotDisposed); /// Depth mode for which this tracker was created. public DepthMode DepthMode { get; } @@ -166,12 +170,18 @@ public bool TryEnqueueCapture(Capture capture, Timeout timeout = default) if (capture is null) throw new ArgumentNullException(nameof(capture)); + if (isDisposed) + throw new ObjectDisposedException(nameof(Tracker)); + var res = NativeApi.TrackerEnqueueCapture(handle.ValueNotDisposed, Capture.ToHandle(capture), timeout); if (res == NativeCallResults.WaitResult.Timeout) return false; if (res == NativeCallResults.WaitResult.Failed) { - handle.CheckNotDisposed(); // to throw ObjectDisposedException() if failure is a result of disposing + // to throw ObjectDisposedException() if failure is a result of disposing + if (isDisposed) + throw new ObjectDisposedException(nameof(Tracker)); + handle.CheckNotDisposed(); using (var depthImage = capture.DepthImage) { @@ -257,6 +267,9 @@ public void EnqueueCapture(Capture capture) /// public bool TryPopResult([NotNullWhen(returnValue: true)] out BodyFrame? bodyFrame, Timeout timeout = default) { + if (isDisposed) + throw new ObjectDisposedException(nameof(Tracker)); + var res = NativeApi.TrackerPopResult(handle.ValueNotDisposed, out var bodyFrameHandle, timeout); if (res == NativeCallResults.WaitResult.Timeout) { @@ -265,7 +278,11 @@ public bool TryPopResult([NotNullWhen(returnValue: true)] out BodyFrame? bodyFra } if (res == NativeCallResults.WaitResult.Failed) { - handle.CheckNotDisposed(); // to throw ObjectDisposedException() if failure is a result of disposing + // to throw ObjectDisposedException() if failure is a result of disposing + if (isDisposed) + throw new ObjectDisposedException(nameof(Tracker)); + handle.CheckNotDisposed(); + throw new BodyTrackingException("Cannot extract tracking result from body tracking pipeline. See logs for details."); } diff --git a/K4AdotNet/K4AdotNet.xml b/K4AdotNet/K4AdotNet.xml index 2a9b75b..b130cec 100644 --- a/K4AdotNet/K4AdotNet.xml +++ b/K4AdotNet/K4AdotNet.xml @@ -799,6 +799,7 @@ This function may be called while another thread is blocking in or . Calling this function while another thread is in that function will result in that function raising an exception. + Object was disposed. Depth mode for which this tracker was created. @@ -7660,149 +7661,3 @@ -System.Diagnostics.CodeAnalysis.AllowNullAttribute"> - - Specifies that is allowed as an input even if the - corresponding type disallows it. - - - - - Initializes a new instance of the class. - - - - - Specifies that is disallowed as an input even if the - corresponding type allows it. - - - - - Initializes a new instance of the class. - - - - - Specifies that a method that will never return under any circumstance. - - - - - Initializes a new instance of the class. - - - - - - Specifies that the method will not return if the associated - parameter is passed the specified value. - - - - - Gets the condition parameter value. - Code after the method is considered unreachable by diagnostics if the argument - to the associated parameter matches this value. - - - - - Initializes a new instance of the - class with the specified parameter value. - - - The condition parameter value. - Code after the method is considered unreachable by diagnostics if the argument - to the associated parameter matches this value. - - - - - Specifies that an output may be even if the - corresponding type disallows it. - - - - - Initializes a new instance of the class. - - - - - Specifies that when a method returns , - the parameter may be even if the corresponding type disallows it. - - - - - Gets the return value condition. - If the method returns this value, the associated parameter may be . - - - - - Initializes the attribute with the specified return value condition. - - - The return value condition. - If the method returns this value, the associated parameter may be . - - - - - Specifies that an output is not even if the - corresponding type allows it. - - - - - Initializes a new instance of the class. - - - - - Specifies that the output will be non- if the - named parameter is non-. - - - - - Gets the associated parameter name. - The output will be non- if the argument to the - parameter specified is non-. - - - - - Initializes the attribute with the associated parameter name. - - - The associated parameter name. - The output will be non- if the argument to the - parameter specified is non-. - - - - - Specifies that when a method returns , - the parameter will not be even if the corresponding type allows it. - - - - - Gets the return value condition. - If the method returns this value, the associated parameter will not be . - - - - - Initializes the attribute with the specified return value condition. - - - The return value condition. - If the method returns this value, the associated parameter will not be . - - - -