Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenCV
opencv
提交
df834597
O
opencv
项目概览
OpenCV
/
opencv
上一次同步 大约 1 年
通知
1005
Star
71102
Fork
55580
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
O
opencv
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
df834597
编写于
8月 25, 2021
作者:
A
Alexander Alekhin
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #20602 from diablodale:fix20594-refcounts-wiith-exceptions
上级
77a5c43d
54a9e009
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
99 addition
and
28 deletion
+99
-28
modules/core/src/matrix.cpp
modules/core/src/matrix.cpp
+2
-3
modules/core/src/ocl.cpp
modules/core/src/ocl.cpp
+24
-2
modules/core/src/umatrix.cpp
modules/core/src/umatrix.cpp
+40
-23
modules/core/test/test_umat.cpp
modules/core/test/test_umat.cpp
+33
-0
未找到文件。
modules/core/src/matrix.cpp
浏览文件 @
df834597
...
...
@@ -749,18 +749,17 @@ Mat::Mat(const Mat& m, const Rect& roi)
data
+=
roi
.
x
*
esz
;
CV_Assert
(
0
<=
roi
.
x
&&
0
<=
roi
.
width
&&
roi
.
x
+
roi
.
width
<=
m
.
cols
&&
0
<=
roi
.
y
&&
0
<=
roi
.
height
&&
roi
.
y
+
roi
.
height
<=
m
.
rows
);
if
(
u
)
CV_XADD
(
&
u
->
refcount
,
1
);
if
(
roi
.
width
<
m
.
cols
||
roi
.
height
<
m
.
rows
)
flags
|=
SUBMATRIX_FLAG
;
step
[
0
]
=
m
.
step
[
0
];
step
[
1
]
=
esz
;
updateContinuityFlag
();
addref
();
if
(
rows
<=
0
||
cols
<=
0
)
{
release
();
rows
=
cols
=
0
;
release
();
}
}
...
...
modules/core/src/ocl.cpp
浏览文件 @
df834597
...
...
@@ -2774,19 +2774,33 @@ struct Kernel::Impl
void
cleanupUMats
()
{
bool
exceptionOccurred
=
false
;
for
(
int
i
=
0
;
i
<
MAX_ARRS
;
i
++
)
{
if
(
u
[
i
]
)
{
if
(
CV_XADD
(
&
u
[
i
]
->
urefcount
,
-
1
)
==
1
)
{
u
[
i
]
->
flags
|=
UMatData
::
ASYNC_CLEANUP
;
u
[
i
]
->
currAllocator
->
deallocate
(
u
[
i
]);
try
{
u
[
i
]
->
currAllocator
->
deallocate
(
u
[
i
]);
}
catch
(
const
std
::
exception
&
exc
)
{
// limited by legacy before C++11, therefore log and
// remember some exception occurred to throw below
CV_LOG_ERROR
(
NULL
,
"OCL: Unexpected C++ exception in OpenCL Kernel::Impl::cleanupUMats(): "
<<
exc
.
what
());
exceptionOccurred
=
true
;
}
}
u
[
i
]
=
0
;
}
}
nu
=
0
;
haveTempDstUMats
=
false
;
haveTempSrcUMats
=
false
;
CV_Assert
(
!
exceptionOccurred
);
}
void
addUMat
(
const
UMat
&
m
,
bool
dst
)
...
...
@@ -2817,8 +2831,16 @@ struct Kernel::Impl
void
finit
(
cl_event
e
)
{
CV_UNUSED
(
e
);
cleanupUMats
();
isInProgress
=
false
;
try
{
cleanupUMats
();
}
catch
(...)
{
release
();
throw
;
}
release
();
}
...
...
modules/core/src/umatrix.cpp
浏览文件 @
df834597
...
...
@@ -540,13 +540,26 @@ UMat Mat::getUMat(int accessFlags, UMatUsageFlags usageFlags) const
CV_XADD
(
&
(
u
->
refcount
),
1
);
CV_XADD
(
&
(
u
->
urefcount
),
1
);
}
hdr
.
flags
=
flags
;
setSize
(
hdr
,
dims
,
size
.
p
,
step
.
p
);
finalizeHdr
(
hdr
);
hdr
.
u
=
new_u
;
hdr
.
offset
=
0
;
//data - datastart;
hdr
.
addref
();
return
hdr
;
try
{
hdr
.
flags
=
flags
;
setSize
(
hdr
,
dims
,
size
.
p
,
step
.
p
);
finalizeHdr
(
hdr
);
hdr
.
u
=
new_u
;
hdr
.
offset
=
0
;
//data - datastart;
hdr
.
addref
();
return
hdr
;
}
catch
(...)
{
if
(
u
!=
NULL
)
{
CV_XADD
(
&
(
u
->
refcount
),
-
1
);
CV_XADD
(
&
(
u
->
urefcount
),
-
1
);
}
new_u
->
currAllocator
->
deallocate
(
new_u
);
throw
;
}
}
void
UMat
::
create
(
int
d
,
const
int
*
_sizes
,
int
_type
,
UMatUsageFlags
_usageFlags
)
...
...
@@ -692,18 +705,17 @@ UMat::UMat(const UMat& m, const Rect& roi)
offset
+=
roi
.
x
*
esz
;
CV_Assert
(
0
<=
roi
.
x
&&
0
<=
roi
.
width
&&
roi
.
x
+
roi
.
width
<=
m
.
cols
&&
0
<=
roi
.
y
&&
0
<=
roi
.
height
&&
roi
.
y
+
roi
.
height
<=
m
.
rows
);
if
(
u
)
CV_XADD
(
&
(
u
->
urefcount
),
1
);
if
(
roi
.
width
<
m
.
cols
||
roi
.
height
<
m
.
rows
)
flags
|=
SUBMATRIX_FLAG
;
step
[
0
]
=
m
.
step
[
0
];
step
[
1
]
=
esz
;
updateContinuityFlag
();
addref
();
if
(
rows
<=
0
||
cols
<=
0
)
{
release
();
rows
=
cols
=
0
;
release
();
}
}
...
...
@@ -969,24 +981,29 @@ Mat UMat::getMat(int accessFlags) const
// TODO Support ACCESS_READ (ACCESS_WRITE) without unnecessary data transfers
accessFlags
|=
ACCESS_RW
;
UMatDataAutoLock
autolock
(
u
);
if
(
CV_XADD
(
&
u
->
refcount
,
1
)
==
0
)
u
->
currAllocator
->
map
(
u
,
accessFlags
);
if
(
u
->
data
!=
0
)
try
{
Mat
hdr
(
dims
,
size
.
p
,
type
(),
u
->
data
+
offset
,
step
.
p
);
hdr
.
flags
=
flags
;
hdr
.
u
=
u
;
hdr
.
datastart
=
u
->
data
;
hdr
.
data
=
u
->
data
+
offset
;
hdr
.
datalimit
=
hdr
.
dataend
=
u
->
data
+
u
->
size
;
return
hdr
;
if
(
CV_XADD
(
&
u
->
refcount
,
1
)
==
0
)
u
->
currAllocator
->
map
(
u
,
accessFlags
);
if
(
u
->
data
!=
0
)
{
Mat
hdr
(
dims
,
size
.
p
,
type
(),
u
->
data
+
offset
,
step
.
p
);
hdr
.
flags
=
flags
;
hdr
.
u
=
u
;
hdr
.
datastart
=
u
->
data
;
hdr
.
data
=
u
->
data
+
offset
;
hdr
.
datalimit
=
hdr
.
dataend
=
u
->
data
+
u
->
size
;
return
hdr
;
}
}
else
catch
(...)
{
CV_XADD
(
&
u
->
refcount
,
-
1
);
CV_Assert
(
u
->
data
!=
0
&&
"Error mapping of UMat to host memory."
);
return
Mat
();
throw
;
}
CV_XADD
(
&
u
->
refcount
,
-
1
);
CV_Assert
(
u
->
data
!=
0
&&
"Error mapping of UMat to host memory."
);
return
Mat
();
}
void
*
UMat
::
handle
(
int
accessFlags
)
const
...
...
modules/core/test/test_umat.cpp
浏览文件 @
df834597
...
...
@@ -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
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录