Crash using Vec with not number value. (Bug #682)
Description
Executing the following code:
cv::Vec<std::string,4> vecStr("Alice","Bob","Ted");
Make the software thrown this error:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct NULL not valid
because we call the constructor with a number in the default constructor: _Tp(0)
in the following line:
template<typename _Tp, int cn> inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2)
{
assert(cn >= 3);
valr0 = v0; valr1 = v1; valr2 = v2;
for(int i = 3; i < cn; i++) val[i] = _Tp(0);
}
It would be better to do something like
template<typename _Tp, int cn> inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2)
{
assert(cn >= 3);
valr0 = v0; valr1 = v1; valr2 = v2;
memset(&valr3,0,(&val[cn]-valr3)); //Assure the set the reserved memory to zeros
for(int i = 3; i < cn; i++) val[i] = _Tp(); //Call the default constructor
}
(Or maybe calling the default constructor let the primitive type set to zeros)
Associated revisions
Merge pull request #682 from vpisarev/python_str_fix
repaired std::string handling
History
Updated by Vadim Pisarevsky over 14 years ago
Vec is designed solely for keeping tuples of primitive numerical types: unsigned/signed char, unsigned/signed short, int, float and double. In other words, for any Vec<> it should be always possible to create an OpenCV multi-channel array, which elements are of those Vec's.
For strings and other complex types, please, use std::vector.
I checked your example with the latest snapshot on OSX 10.6 with GCC 4.2 and it failed to compile Vec<string, 4> declaration, which is what should really happen. If it compiles fine on your machine, please, specify your OS and the compiler version. I will try to make it always fail at compile stage, not at runtime.
- Status changed from Open to Done
- (deleted custom field) set to wontfix