diff --git a/modules/core/src/matrix.cpp b/modules/core/src/matrix.cpp index 3480fffe78..98d25d909f 100644 --- a/modules/core/src/matrix.cpp +++ b/modules/core/src/matrix.cpp @@ -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) { diff --git a/modules/core/test/test_mat.cpp b/modules/core/test/test_mat.cpp index b0fcea60c6..33f438d0c4 100644 --- a/modules/core/test/test_mat.cpp +++ b/modules/core/test/test_mat.cpp @@ -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 class Core_Mat_copyTo : public testing::Test {}; +TYPED_TEST_CASE_P(Core_Mat_copyTo); + +TYPED_TEST_P(Core_Mat_copyTo, EmptyFixedType) +{ + cv::Mat_ a; + cv::Mat_ 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::value); +} + +REGISTER_TYPED_TEST_CASE_P(Core_Mat_copyTo, EmptyFixedType); +typedef ::testing::Types AllMatDepths; +INSTANTIATE_TYPED_TEST_CASE_P(CopyToTest, Core_Mat_copyTo, AllMatDepths); + TEST(Core_Mat, copyNx1ToVector) { cv::Mat_ src(5, 1);