From 5f20f48a5bbafa8015f3a7eafe278d7541c0e3fd Mon Sep 17 00:00:00 2001 From: jiajia Qian Date: Thu, 5 Nov 2026 17:15:44 +0800 Subject: [PATCH] Fix(core): correct UMat mapcount handling in OpenCL path `u->mapcount` was incremented before `clEnqueueMapBuffer()`. If mapping failed, the counter remained incremented even though no mapping was established, leaving `UMatData` in an inconsistent state and potentially triggering later assertions such as: (-215:Assertion failed) u->mapcount == 0 in function 'deallocate' Update the logic to increment `mapcount` only after a successful `clEnqueueMapBuffer()` call. The issue was observed while running: OCL_PyrDownFixture_PyrDown.PyrDown/6 from opencv_perf_imgproc. Assisted-by: LLM Signed-off-by: jiajia Qian --- modules/core/src/ocl.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/modules/core/src/ocl.cpp b/modules/core/src/ocl.cpp index 7d3a13a855..1ed99b51a9 100644 --- a/modules/core/src/ocl.cpp +++ b/modules/core/src/ocl.cpp @@ -5922,10 +5922,12 @@ public: if (!u->deviceMemMapped()) { CV_Assert(u->refcount == 1); - CV_Assert(u->mapcount++ == 0); + CV_Assert(u->mapcount == 0); u->data = (uchar*)clEnqueueMapBuffer(q, (cl_mem)u->handle, CL_TRUE, (CL_MAP_READ | CL_MAP_WRITE), 0, u->size, 0, 0, 0, &retval); + if (u->data && retval == CL_SUCCESS) + ++u->mapcount; CV_OCL_DBG_CHECK_RESULT(retval, cv::format("clEnqueueMapBuffer(handle=%p, sz=%lld) => %p", (void*)u->handle, (long long int)u->size, u->data).c_str()); } if (u->data && retval == CL_SUCCESS) @@ -6004,8 +6006,10 @@ public: #endif if (u->refcount == 0) { - CV_Assert(u->mapcount-- == 1); + CV_Assert(u->mapcount == 1); retval = clEnqueueUnmapMemObject(q, (cl_mem)u->handle, u->data, 0, 0, 0); + if (retval == CL_SUCCESS) + --u->mapcount; CV_OCL_CHECK_RESULT(retval, cv::format("clEnqueueUnmapMemObject(handle=%p, data=%p, [sz=%lld])", (void*)u->handle, u->data, (long long int)u->size).c_str()); if (Device::getDefault().isAMD()) {