Skip to content

Commit

Permalink
Minor improvements in releasing of Body Tracking resources
Browse files Browse the repository at this point in the history
  • Loading branch information
bibigone committed Sep 30, 2020
1 parent dc1bb86 commit 3f18ad9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 156 deletions.
4 changes: 2 additions & 2 deletions K4AdotNet.Samples.Unity/Assets/Scripts/SkeletonProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ private IEnumerator Start()

private void OnDestroy()
{
IsAvailable = false;

var captureManager = FindObjectOfType<CaptureManager>();
if (captureManager != null) captureManager.CaptureReady -= CaptureManager_CaptureReady;
_tracker?.Dispose();

IsAvailable = false;
}

private void CaptureManager_CaptureReady(object sender, CaptureEventArgs e)
Expand Down
33 changes: 25 additions & 8 deletions K4AdotNet/BodyTracking/Tracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public sealed class Tracker : IDisposablePlus
{
private readonly NativeHandles.HandleWrapper<NativeHandles.TrackerHandle> 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();

Expand Down Expand Up @@ -68,11 +69,16 @@ private void Handle_Disposed(object sender, EventArgs e)
/// <seealso cref="Disposed"/>
/// <seealso cref="IsDisposed"/>
public void Dispose()
=> handle.Dispose();
{
isDisposed = true;
if (!handle.IsDisposed)
Shutdown();
handle.Dispose();
}

/// <summary>Gets a value indicating whether the object has been disposed of.</summary>
/// <seealso cref="Dispose"/>
public bool IsDisposed => handle.IsDisposed;
public bool IsDisposed => isDisposed;

/// <summary>Raised on object disposing (only once).</summary>
/// <seealso cref="Dispose"/>
Expand All @@ -89,11 +95,9 @@ public void Dispose()
/// This function may be called while another thread is blocking in <see cref="TryEnqueueCapture(Capture, Timeout)"/> or <see cref="TryPopResult(out BodyFrame, Timeout)"/>.
/// Calling this function while another thread is in that function will result in that function raising an exception.
/// </para></remarks>
/// <exception cref="ObjectDisposedException">Object was disposed.</exception>
public void Shutdown()
{
NativeApi.TrackerShutdown(handle.Value);
queueSize = 0;
}
=> NativeApi.TrackerShutdown(handle.ValueNotDisposed);

/// <summary>Depth mode for which this tracker was created.</summary>
public DepthMode DepthMode { get; }
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -257,6 +267,9 @@ public void EnqueueCapture(Capture capture)
/// <seealso cref="PopResult"/>
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)
{
Expand All @@ -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.");
}

Expand Down
147 changes: 1 addition & 146 deletions K4AdotNet/K4AdotNet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@
This function may be called while another thread is blocking in <see cref="M:K4AdotNet.BodyTracking.Tracker.TryEnqueueCapture(K4AdotNet.Sensor.Capture,K4AdotNet.Timeout)"/> or <see cref="M:K4AdotNet.BodyTracking.Tracker.TryPopResult(K4AdotNet.BodyTracking.BodyFrame@,K4AdotNet.Timeout)"/>.
Calling this function while another thread is in that function will result in that function raising an exception.
</para></remarks>
<exception cref="T:System.ObjectDisposedException">Object was disposed.</exception>
</member>
<member name="P:K4AdotNet.BodyTracking.Tracker.DepthMode">
<summary>Depth mode for which this tracker was created.</summary>
Expand Down Expand Up @@ -7660,149 +7661,3 @@
</member>
</members>
</doc>
System.Diagnostics.CodeAnalysis.AllowNullAttribute">
<summary>
Specifies that <see langword="null"/> is allowed as an input even if the
corresponding type disallows it.
</summary>
</member>
<member name="M:System.Diagnostics.CodeAnalysis.AllowNullAttribute.#ctor">
<summary>
Initializes a new instance of the <see cref="T:System.Diagnostics.CodeAnalysis.AllowNullAttribute"/> class.
</summary>
</member>
<member name="T:System.Diagnostics.CodeAnalysis.DisallowNullAttribute">
<summary>
Specifies that <see langword="null"/> is disallowed as an input even if the
corresponding type allows it.
</summary>
</member>
<member name="M:System.Diagnostics.CodeAnalysis.DisallowNullAttribute.#ctor">
<summary>
Initializes a new instance of the <see cref="T:System.Diagnostics.CodeAnalysis.DisallowNullAttribute"/> class.
</summary>
</member>
<member name="T:System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute">
<summary>
Specifies that a method that will never return under any circumstance.
</summary>
</member>
<member name="M:System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute.#ctor">
<summary>
Initializes a new instance of the <see cref="T:System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute"/> class.
</summary>

</member>
<member name="T:System.Diagnostics.CodeAnalysis.DoesNotReturnIfAttribute">
<summary>
Specifies that the method will not return if the associated <see cref="T:System.Boolean"/>
parameter is passed the specified value.
</summary>
</member>
<member name="P:System.Diagnostics.CodeAnalysis.DoesNotReturnIfAttribute.ParameterValue">
<summary>
Gets the condition parameter value.
Code after the method is considered unreachable by diagnostics if the argument
to the associated parameter matches this value.
</summary>
</member>
<member name="M:System.Diagnostics.CodeAnalysis.DoesNotReturnIfAttribute.#ctor(System.Boolean)">
<summary>
Initializes a new instance of the <see cref="T:System.Diagnostics.CodeAnalysis.DoesNotReturnIfAttribute"/>
class with the specified parameter value.
</summary>
<param name="parameterValue">
The condition parameter value.
Code after the method is considered unreachable by diagnostics if the argument
to the associated parameter matches this value.
</param>
</member>
<member name="T:System.Diagnostics.CodeAnalysis.MaybeNullAttribute">
<summary>
Specifies that an output may be <see langword="null"/> even if the
corresponding type disallows it.
</summary>
</member>
<member name="M:System.Diagnostics.CodeAnalysis.MaybeNullAttribute.#ctor">
<summary>
Initializes a new instance of the <see cref="T:System.Diagnostics.CodeAnalysis.MaybeNullAttribute"/> class.
</summary>
</member>
<member name="T:System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute">
<summary>
Specifies that when a method returns <see cref="P:System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute.ReturnValue"/>,
the parameter may be <see langword="null"/> even if the corresponding type disallows it.
</summary>
</member>
<member name="P:System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute.ReturnValue">
<summary>
Gets the return value condition.
If the method returns this value, the associated parameter may be <see langword="null"/>.
</summary>
</member>
<member name="M:System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute.#ctor(System.Boolean)">
<summary>
Initializes the attribute with the specified return value condition.
</summary>
<param name="returnValue">
The return value condition.
If the method returns this value, the associated parameter may be <see langword="null"/>.
</param>
</member>
<member name="T:System.Diagnostics.CodeAnalysis.NotNullAttribute">
<summary>
Specifies that an output is not <see langword="null"/> even if the
corresponding type allows it.
</summary>
</member>
<member name="M:System.Diagnostics.CodeAnalysis.NotNullAttribute.#ctor">
<summary>
Initializes a new instance of the <see cref="T:System.Diagnostics.CodeAnalysis.NotNullAttribute"/> class.
</summary>
</member>
<member name="T:System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute">
<summary>
Specifies that the output will be non-<see langword="null"/> if the
named parameter is non-<see langword="null"/>.
</summary>
</member>
<member name="P:System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute.ParameterName">
<summary>
Gets the associated parameter name.
The output will be non-<see langword="null"/> if the argument to the
parameter specified is non-<see langword="null"/>.
</summary>
</member>
<member name="M:System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute.#ctor(System.String)">
<summary>
Initializes the attribute with the associated parameter name.
</summary>
<param name="parameterName">
The associated parameter name.
The output will be non-<see langword="null"/> if the argument to the
parameter specified is non-<see langword="null"/>.
</param>
</member>
<member name="T:System.Diagnostics.CodeAnalysis.NotNullWhenAttribute">
<summary>
Specifies that when a method returns <see cref="P:System.Diagnostics.CodeAnalysis.NotNullWhenAttribute.ReturnValue"/>,
the parameter will not be <see langword="null"/> even if the corresponding type allows it.
</summary>
</member>
<member name="P:System.Diagnostics.CodeAnalysis.NotNullWhenAttribute.ReturnValue">
<summary>
Gets the return value condition.
If the method returns this value, the associated parameter will not be <see langword="null"/>.
</summary>
</member>
<member name="M:System.Diagnostics.CodeAnalysis.NotNullWhenAttribute.#ctor(System.Boolean)">
<summary>
Initializes the attribute with the specified return value condition.
</summary>
<param name="returnValue">
The return value condition.
If the method returns this value, the associated parameter will not be <see langword="null"/>.
</param>
</member>
</members>
</doc>

0 comments on commit 3f18ad9

Please sign in to comment.