Skip to content

Commit fd2a406

Browse files
authored
Fix: Set Buffer data via rvalue instead of lvalue (#1817)
* Set rvalues instead of lvalues to Buffer data to avoid incorrect VectorMemory sizes. * Add comments to describe the reason for setting a rvalue
1 parent 5fe9eaa commit fd2a406

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

src/pipeline/datatype/ImgDetectionsT.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <array>
55
#include <cstring>
66
#include <stdexcept>
7+
#include <utility>
78
#include <vector>
89

910
#include "depthai/pipeline/datatype/ImgDetections.hpp"
@@ -27,7 +28,8 @@ void ImgDetectionsT<DetectionT>::setSegmentationMask(const std::vector<std::uint
2728
if(mask.size() != width * height) {
2829
throw std::runtime_error("SegmentationMask: data size does not match width*height");
2930
}
30-
setData(mask);
31+
auto vecMask = mask; // Avoid mutating shared storage by moving a copy of the input into a new buffer
32+
setData(std::move(vecMask)); // Call the rvalue overload to allocate a new memory holder
3133
this->segmentationMaskWidth = width;
3234
this->segmentationMaskHeight = height;
3335
}
@@ -39,7 +41,7 @@ void ImgDetectionsT<DetectionT>::setSegmentationMask(dai::ImgFrame& frame) {
3941
}
4042
auto dataSpan = frame.getData();
4143
std::vector<std::uint8_t> vecMask(dataSpan.begin(), dataSpan.end());
42-
setData(vecMask);
44+
setData(std::move(vecMask)); // Call the rvalue overload to allocate a new memory holder
4345
this->segmentationMaskWidth = frame.getWidth();
4446
this->segmentationMaskHeight = frame.getHeight();
4547
}
@@ -87,7 +89,7 @@ void ImgDetectionsT<DetectionT>::setCvSegmentationMask(cv::Mat mask) {
8789
} else {
8890
dataVec.insert(dataVec.begin(), mask.datastart, mask.dataend);
8991
}
90-
setData(dataVec);
92+
setData(std::move(dataVec)); // Call the rvalue overload to allocate a new memory holder
9193
this->segmentationMaskWidth = mask.cols;
9294
this->segmentationMaskHeight = mask.rows;
9395
}

src/pipeline/datatype/SegmentationMask.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <cstring>
66
#include <optional>
77
#include <stdexcept>
8+
#include <utility>
89
#include <vector>
910

1011
#include "depthai/common/RotatedRect.hpp"
@@ -43,7 +44,8 @@ void SegmentationMask::setMask(const std::vector<std::uint8_t>& mask, size_t wid
4344
if(mask.size() != width * height) {
4445
throw std::runtime_error("SegmentationMask: data size does not match width*height");
4546
}
46-
setData(mask);
47+
auto vecMask = mask; // Avoid mutating shared storage by moving a copy of the input into a new buffer
48+
setData(std::move(vecMask)); // Call the rvalue overload to allocate a new memory holder
4749
this->width = width;
4850
this->height = height;
4951
}
@@ -240,7 +242,7 @@ void SegmentationMask::setCvMask(cv::Mat mask) {
240242
} else {
241243
dataVec.insert(dataVec.begin(), mask.datastart, mask.dataend);
242244
}
243-
setData(dataVec);
245+
setData(std::move(dataVec)); // Call the rvalue overload to allocate a new memory holder
244246
this->width = mask.cols;
245247
this->height = mask.rows;
246248
}

0 commit comments

Comments
 (0)