Compare commits

...

2 Commits

Author SHA1 Message Date
Alexander Smorkalov
ccf4ca3243 Merge pull request #30676 from Lurie97:fix_mapcount
Fix(core): correct UMat mapcount handling in OpenCL path
2026-11-07 21:24:40 +03:00
jiajia Qian
5f20f48a5b 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 <jiajia.qian@nxp.com>
2026-11-05 17:43:10 +08:00

View File

@@ -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())
{