getUMat does not work properly with Mat on user data (Bug #4006)
Description
1. The following code raises a exception
void* user_data = malloc(1024*1024);
UMat m = Mat(1024,1024,CV_8U,user_data).getUMat(ACCESS_RW);
{
Mat t = m.getMat(ACCESS_RW);
}
m.setTo(0); //exception
2. the Mat.getUMat in case of Mat.u==NULL creates umatdata with refcount=1 but without reference from any Mat object. So the created umatdata at least !!will never be released.
UMat Mat::getUMat(int accessFlags, UMatUsageFlags usageFlags) const
{
UMat hdr;
if(!data)
return hdr;
UMatData* temp_u = u;
if(!temp_u) // true for Mat(,,,user_data)
{
MatAllocator a = allocator, *a0 = getStdAllocator();
if(!a)
a = a0;
temp_u = a->allocate(dims, size.p, type(), data, step.p, accessFlags, usageFlags);
*temp_u->refcount = 1; //temp_u is not referenced from any Mat but refcount=1
}
UMat::getStdAllocator()->allocate(temp_u, accessFlags, usageFlags);
hdr.flags = flags;
setSize(hdr, dims, size.p, step.p);
finalizeHdr(hdr);
hdr.u = temp_u;
hdr.offset = data - datastart;
hdr.addref();
return hdr;
}
Related issues
related to Bug #4380: Mat/UMat may be out of sync. | New | 2015-06-04 |
Associated revisions
Merge pull request #4006 from sgjava:opencv-steve
Revert of "Merge pull request #4006 from sgjava:opencv-steve" (reverted from commit 4743184078659d4bcf43c363efd2aa430e4786ff)
History
Updated by Ilya Lavrenov over 10 years ago
Hi Konstantin,
The first example is incorrect, since life time of UMat must be longer than Mat. But in your example Mat is temp object and so UMat is invalid and you have a strange behaviour.
Updated by Ilya Lavrenov over 10 years ago
- Priority changed from Normal to High
- Status changed from New to Open
Updated by Konstantin Rodyushkin about 10 years ago
Ilya Lavrenov wrote:
Hi Konstantin,
The first example is incorrect, since life time of UMat must be longer than Mat. But in your example Mat is temp object and so UMat is invalid and you have a strange behaviour.
Then it make sense to report about this in the incorrect line "UMat m = Mat(1024,1024,CV_8U,user_data).getUMat(ACCESS_RW);"
but not in "m.setTo(0);" Isn't it?
Updated by Vadim Pisarevsky almost 10 years ago
- Priority changed from High to Normal
- Category set to t-api
Updated by Konstantin Rodyushkin almost 10 years ago
Ilya Lavrenov wrote:
Hi Konstantin,
The first example is incorrect, since life time of UMat must be longer than Mat. But in your example Mat is temp object and so UMat is invalid and you have a strange behaviour.
I am sorry for late comment but I just found similar example in the test code
TEST
{
{
Mat m(10, 10, CV_8UC1), ref;
Rect r(1, 1, 8, 8);
{
UMat um = m(r).getUMat(ACCESS_WRITE);
um.setTo(17);
}
}
}
m(r) is also temporal object.
Does it mean that getUMat is incorrectly used in TEST?
Updated by Maksim Shabunin over 9 years ago
Issue has been transferred to GitHub: https://github.com/Itseez/opencv/issues/4862