Skip to content

Commit

Permalink
fix(sample): dispose OutputStream if only it exists (#564)
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler authored May 12, 2022
1 parent f4301af commit dcee215
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public override void StartRun(ImageSource imageSource)

public override void Stop()
{
_trackedDetectionsStream.RemoveAllListeners();
_trackedDetectionsStream?.Close();
_trackedDetectionsStream = null;
base.Stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public override void StartRun(ImageSource imageSource)

public override void Stop()
{
_faceDetectionsStream.RemoveAllListeners();
_faceDetectionsStream?.Close();
_faceDetectionsStream = null;
base.Stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ public override void StartRun(ImageSource imageSource)

public override void Stop()
{
_faceDetectionsStream.RemoveAllListeners();
_faceDetectionsStream?.Close();
_faceDetectionsStream = null;
_multiFaceLandmarksStream.RemoveAllListeners();
_multiFaceLandmarksStream?.Close();
_multiFaceLandmarksStream = null;
_faceRectsFromLandmarksStream.RemoveAllListeners();
_faceRectsFromLandmarksStream?.Close();
_faceRectsFromLandmarksStream = null;
_faceRectsFromDetectionsStream.RemoveAllListeners();
_faceRectsFromDetectionsStream?.Close();
_faceRectsFromDetectionsStream = null;
base.Stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override void StartRun(ImageSource imageSource)

public override void Stop()
{
_hairMaskStream.RemoveAllListeners();
_hairMaskStream?.Close();
_hairMaskStream = null;
base.Stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,17 @@ public override void StartRun(ImageSource imageSource)

public override void Stop()
{
_palmDetectionsStream.RemoveAllListeners();
_palmDetectionsStream?.Close();
_palmDetectionsStream = null;
_handRectsFromPalmDetectionsStream.RemoveAllListeners();
_handRectsFromPalmDetectionsStream?.Close();
_handRectsFromPalmDetectionsStream = null;
_handLandmarksStream.RemoveAllListeners();
_handLandmarksStream?.Close();
_handLandmarksStream = null;
_handWorldLandmarksStream.RemoveAllListeners();
_handWorldLandmarksStream?.Close();
_handWorldLandmarksStream = null;
_handRectsFromLandmarksStream.RemoveAllListeners();
_handRectsFromLandmarksStream?.Close();
_handRectsFromLandmarksStream = null;
_handednessStream.RemoveAllListeners();
_handednessStream?.Close();
_handednessStream = null;
base.Stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,21 @@ public override void StartRun(ImageSource imageSource)

public override void Stop()
{
_poseDetectionStream.RemoveAllListeners();
_poseDetectionStream?.Close();
_poseDetectionStream = null;
_poseLandmarksStream.RemoveAllListeners();
_poseLandmarksStream?.Close();
_poseLandmarksStream = null;
_faceLandmarksStream.RemoveAllListeners();
_faceLandmarksStream?.Close();
_faceLandmarksStream = null;
_leftHandLandmarksStream.RemoveAllListeners();
_leftHandLandmarksStream?.Close();
_leftHandLandmarksStream = null;
_rightHandLandmarksStream.RemoveAllListeners();
_rightHandLandmarksStream?.Close();
_rightHandLandmarksStream = null;
_poseWorldLandmarksStream.RemoveAllListeners();
_poseWorldLandmarksStream?.Close();
_poseWorldLandmarksStream = null;
_segmentationMaskStream.RemoveAllListeners();
_segmentationMaskStream?.Close();
_segmentationMaskStream = null;
_poseRoiStream.RemoveAllListeners();
_poseRoiStream?.Close();
_poseRoiStream = null;
base.Stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public override void StartRun(ImageSource imageSource)

public override void Stop()
{
_trackedAnchorDataStream.RemoveAllListeners();
_trackedAnchorDataStream?.Close();
_trackedAnchorDataStream = null;
base.Stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public override void StartRun(ImageSource imageSource)

public override void Stop()
{
_faceDetectionsStream.RemoveAllListeners();
_faceDetectionsStream?.Close();
_faceDetectionsStream = null;
_faceRectStream.RemoveAllListeners();
_faceRectStream?.Close();
_faceRectStream = null;
_faceLandmarksWithIrisStream.RemoveAllListeners();
_faceLandmarksWithIrisStream?.Close();
_faceLandmarksWithIrisStream = null;
base.Stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public override void StartRun(ImageSource imageSource)

public override void Stop()
{
_outputVideoStream.RemoveAllListeners();
_outputVideoStream?.Close();
_outputVideoStream = null;
base.Stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public override void StartRun(ImageSource imageSource)

public override void Stop()
{
_outputDetectionsStream.RemoveAllListeners();
_outputDetectionsStream?.Close();
_outputDetectionsStream = null;
base.Stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ public override void StartRun(ImageSource imageSource)

public override void Stop()
{
_liftedObjectsStream.RemoveAllListeners();
_liftedObjectsStream?.Close();
_liftedObjectsStream = null;
_multiBoxRectsStream.RemoveAllListeners();
_multiBoxRectsStream?.Close();
_multiBoxRectsStream = null;
_multiBoxLandmarksStream.RemoveAllListeners();
_multiBoxLandmarksStream?.Close();
_multiBoxLandmarksStream = null;
base.Stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ public override void StartRun(ImageSource imageSource)

public override void Stop()
{
_poseDetectionStream.RemoveAllListeners();
_poseDetectionStream?.Close();
_poseDetectionStream = null;
_poseLandmarksStream.RemoveAllListeners();
_poseLandmarksStream?.Close();
_poseLandmarksStream = null;
_poseWorldLandmarksStream.RemoveAllListeners();
_poseWorldLandmarksStream?.Close();
_poseWorldLandmarksStream = null;
_segmentationMaskStream.RemoveAllListeners();
_segmentationMaskStream?.Close();
_segmentationMaskStream = null;
_roiFromLandmarksStream.RemoveAllListeners();
_roiFromLandmarksStream?.Close();
_roiFromLandmarksStream = null;
base.Stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public override void StartRun(ImageSource imageSource)

public override void Stop()
{
_segmentationMaskStream.RemoveAllListeners();
_segmentationMaskStream?.Close();
_segmentationMaskStream = null;
base.Stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,25 @@ public void RemoveAllListeners()
OnReceived = null;
}

public void Close()
{
RemoveAllListeners();
RemoveCallbackStatus();

_poller?.Dispose();
_poller = null;
_outputPacket?.Dispose();
_outputPacket = null;

_presencePoller?.Dispose();
_presencePoller = null;
_presencePacket?.Dispose();
_presencePacket = null;

_referencePacket?.Dispose();
_referencePacket = null;
}

/// <summary>
/// Gets the next value from the stream.
/// This method drops a packet whose timestamp is less than <paramref name="timestampThreshold" />.
Expand Down Expand Up @@ -432,6 +451,21 @@ protected IntPtr GetPinnedStatusPtr(Status status)
return _callbackStatus.mpPtr;
}

protected void RemoveCallbackStatus()
{
_callbackStatus?.Dispose();
_callbackStatus = null;

lock (((ICollection)_CallbackStatus).SyncRoot)
{
if (_CallbackStatus.TryGetValue(_id, out var status))
{
status.Dispose();
}
var _ = _CallbackStatus.Remove(_id);
}
}

protected static void CompressCallbackStatus()
{
lock (((ICollection)_CallbackStatus).SyncRoot)
Expand Down

0 comments on commit dcee215

Please sign in to comment.