0
0
mirror of https://github.com/opencv/opencv.git synced 2026-01-18 17:21:42 +01:00

Fix copyTo on empty fixed-type matrices (#28343)

PR #27972 added _dst.create(size(), type()) in copyTo's empty() block.
In Debug builds, Mat::release() was resetting flags to MAGIC_VAL,
clearing the type information and causing assertion failures when
destination has fixedType().

Preserve type flags in Mat::release() debug mode by using:
  flags = (flags & CV_MAT_TYPE_MASK) | MAGIC_VAL

Thanks to @akretz for suggesting this better approach.
This commit is contained in:
WalkingDevFlag
2026-01-05 14:34:38 +05:30
parent f3758f40ae
commit 175dd57bdd
2 changed files with 20 additions and 1 deletions

View File

@@ -553,7 +553,7 @@ void Mat::release()
for(int i = 0; i < dims; i++)
size.p[i] = 0;
#ifdef _DEBUG
flags = MAGIC_VAL;
flags = (flags & CV_MAT_TYPE_MASK) | MAGIC_VAL;
dims = rows = cols = 0;
if(step.p != step.buf)
{

View File

@@ -1394,6 +1394,25 @@ TEST(Core_Mat, copyToConvertTo_Empty)
ASSERT_EQ(C.type(), CV_32SC2);
}
// Regression test for https://github.com/opencv/opencv/issues/28343
// copyTo on empty fixed-type matrices should be a no-op and succeed
template <typename T> class Core_Mat_copyTo : public testing::Test {};
TYPED_TEST_CASE_P(Core_Mat_copyTo);
TYPED_TEST_P(Core_Mat_copyTo, EmptyFixedType)
{
cv::Mat_<TypeParam> a;
cv::Mat_<TypeParam> b;
EXPECT_NO_THROW(a.copyTo(b));
EXPECT_TRUE(b.empty());
// Verify type is still consistent after copyTo
EXPECT_EQ(b.type(), cv::traits::Type<TypeParam>::value);
}
REGISTER_TYPED_TEST_CASE_P(Core_Mat_copyTo, EmptyFixedType);
typedef ::testing::Types<uchar, schar, ushort, short, int, float, double> AllMatDepths;
INSTANTIATE_TYPED_TEST_CASE_P(CopyToTest, Core_Mat_copyTo, AllMatDepths);
TEST(Core_Mat, copyNx1ToVector)
{
cv::Mat_<uchar> src(5, 1);