add test case
authorDale Phurrough <dale@hidale.com>
Tue, 24 Aug 2021 18:06:36 +0000 (20:06 +0200)
committerDale Phurrough <dale@hidale.com>
Wed, 25 Aug 2021 12:32:40 +0000 (14:32 +0200)
modules/core/test/test_umat.cpp

index c2bd6ec..c323d17 100644 (file)
@@ -1419,4 +1419,37 @@ TEST(UMat, resize_Mat_issue_13577)
     cv::ocl::setUseOpenCL(useOCL);  // restore state
 }
 
+TEST(UMat, exceptions_refcounts_issue_20594)
+{
+    if (!cv::ocl::useOpenCL())
+    {
+        // skip test, difficult to create exception scenario without OpenCL
+        std::cout << "OpenCL is not enabled. Skip test" << std::endl;
+        return;
+    }
+
+    UMat umat1(10, 10, CV_8UC1);
+    EXPECT_EQ(0, umat1.u->refcount);
+
+    // cause exception in underlying allocator
+    void* const original_handle = umat1.u->handle;
+    umat1.u->handle = NULL;
+    try
+    {
+        Mat mat1 = umat1.getMat(ACCESS_RW);
+    }
+    catch (...)
+    {
+        // nothing
+    }
+
+    // check for correct refcount, and no change of intentional bad handle
+    EXPECT_EQ(0, umat1.u->refcount);
+    EXPECT_EQ(NULL, umat1.u->handle);
+
+    // reset UMat to good state
+    umat1.u->refcount = 0;
+    umat1.u->handle = original_handle;
+}
+
 } } // namespace opencv_test::ocl