Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dispose native resources of segmentation masks properly #1166

Merged
merged 3 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public static void Get(this Packet<List<Image>> packet, List<Image> value)

foreach (var imagePtr in imageArray.AsReadOnlySpan())
{
value.Add(new Image(imagePtr, false));
value.Add(new Image(imagePtr, true));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class PoseLandmarkerResultAnnotationController : AnnotationController<Mul
{
[SerializeField] private bool _visualizeZ = false;

private readonly object _currentTargetLock = new object();
private PoseLandmarkerResult _currentTarget;

public void InitScreen(int maskWidth, int maskHeight) => annotation.InitMask(maskWidth, maskHeight);
Expand All @@ -30,7 +31,7 @@ public void DrawNow(PoseLandmarkerResult target)

protected void UpdateCurrentTarget(PoseLandmarkerResult newTarget)
{
if (IsTargetChanged(newTarget, _currentTarget))
lock (_currentTargetLock)
{
newTarget.CloneTo(ref _currentTarget);
isStale = true;
Expand All @@ -39,17 +40,20 @@ protected void UpdateCurrentTarget(PoseLandmarkerResult newTarget)

protected override void SyncNow()
{
isStale = false;
if (_currentTarget.segmentationMasks != null)
lock (_currentTargetLock)
{
ReadMask(_currentTarget.segmentationMasks);
// TODO: stop disposing masks here
foreach (var mask in _currentTarget.segmentationMasks)
isStale = false;
if (_currentTarget.segmentationMasks != null)
{
mask.Dispose();
ReadMask(_currentTarget.segmentationMasks);
// TODO: stop disposing masks here
foreach (var mask in _currentTarget.segmentationMasks)
{
mask.Dispose();
}
}
annotation.Draw(_currentTarget.poseLandmarks, _visualizeZ);
}
annotation.Draw(_currentTarget.poseLandmarks, _visualizeZ);
}
}
}
2 changes: 1 addition & 1 deletion mediapipe_api/framework/formats/image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ MpReturnCode mp_Packet__GetImageVector(mediapipe::Packet* packet, mp_api::Struct
auto data = new mediapipe::Image*[size];

for (auto i = 0; i < size; ++i) {
data[i] = new mediapipe::Image(std::move(vec[i]));
data[i] = new mediapipe::Image(vec[i].GetImageFrameSharedPtr());
}
value_out->data = data;
value_out->size = static_cast<int>(size);
Expand Down
Loading