Assignment with incompatible sizes fails silently (Bug #1714)
Description
I had two matrices one of them with size 2x2, the other one 1x3. When I tried to assign the second one to the first row of 2x2 matrix, it neither raised an error, nor made an assignment. It took me a little while to find out the sizes are incompatible. It is better to raise an error so the errors can be caught early.
Example code to reproduce:
Mat1f A = Mat1f::zeros(2,2); Mat1f B = Mat1f::ones(1,3); A.row(0) = B.colRange(0,3); cout << A << endl;
Associated revisions
Merge pull request #1714 from jet47:find-cuda-fix
Fixed CMake warnings/bugs caused by #1670 and #1714
include() doesn't create a variable scope, so the settings of
CMAKE_MODULE_PATH and CMAKE_FIND_ROOT_PATH_MODE_* were polluting
everything included after OpenCVDetectCUDA.cmake.
Also, FindCUDA includes FindPackageHandleStandardArgs, which includes
CMakeParseArguments, which causes warnings related to policy CMP0017.
Setting it to NEW seems safe enough.
History
Updated by Andrey Kamaev almost 13 years ago
It is a "feature" of C++ API: http://opencv.itseez.com/modules/core/doc/intro.html#automatic-memory-management
A.row(0)
creates a temporary Mat
, then = B.colRange(0,3)
repoints this temporary Mat
to the submat of B
.
To make a real assignment you should use copyTo
method:
B.colRange(0,3).copyTo(A.row(0));
This will rise an error if the sizes are incompatible.
- Status changed from Open to Cancelled
- Target version set to 2.4.0
- Category set to core
- Assignee set to Andrey Kamaev
Updated by Baris Demiroz almost 13 years ago
Doing
B.colRange(0,3).copyTo(A.row(0));does not work either. When 'A' is displayed it is all zero matrix.
So this is still a bug. Can you reopen it?
Updated by Andrey Kamaev almost 13 years ago
Sorry for fast closing, I was not aware about MS language extension which results in unexpected behavior.
The issue is completely fixed in r7625.
From now code
1B.colRange(0,3).copyTo(A.row(0));
compiles both with gcc and MS compiler and assign or throw.
However the following fragment does not throw on incompatible types. And this is intentional gotcha:
1Mat C = A.row(0);
2B.colRange(0,3).copyTo(C);
- Status changed from Cancelled to Done
- Target version deleted (
2.4.0)
Updated by Alexander Shishkov almost 13 years ago
- Target version set to 2.4.0