c++ - Why does this OpenCV code segfault? -
i'm preparing parallelize of c++ opencv code. i've come across segfault, , can't understand what's happening.
here code:
class exhaustivecomparisonmt { vector<mat> *m_sigs; mat *m_dist; public: exhaustivecomparisonmt(vector<mat> sigs, mat dist) { m_sigs = &sigs; m_dist = &dist; // gdb breakpoint 1 here } void operator() (size_t last_row, size_t last_col) const { mat diff = (*m_sigs)[0].clone(); // segfault happens here, gdb breakpoint 2 here (size_t = 0; != last_row; ++i) (size_t j = 0; j != last_row; ++j) { cv::absdiff((*m_sigs)[i], (*m_sigs)[j], diff); m_dist->at<double>(i, j) = cv::sum(diff).val[0]; } } }; void exhaustive_comparison(vector<mat> sigs, mat dist) { size_t width = sigs.size(); exhaustivecomparisonmt ecmt(sigs, dist); ecmt(width, width); }
basically, vector of matrices being passed constructor. pointer vector kept member variable, vector can accessed again in exhaustive_comparison
. however, function trips trying access first element of vector.
i've tried diagnose problem gdb putting 2 breakpoints (see code). @ breakpoint 1:
(gdb) p (*m_sigs)[0] $1 = (cv::mat &) @0x7fffee77d010: {flags = 1124024325, dims = 2, rows = 1, cols = 712, data = 0x624ec0 "", refcount = 0x0, datastart = 0x624ec0 "", dataend = 0x6259e0 "", datalimit = 0x6259e0 "", allocator = 0x0, size = {p = 0x7fffee77d018}, step = {p = 0x7fffee77d060, buf = {2848, 4}}}
therefore, first element being accessed correctly. now, go breakpoint 2 , try same thing:
(gdb) p (*m_sigs)[0] $2 = (cv::mat &) @0x7fffee77d010: <error reading variable>
the first element appears no longer accessible! address same (0x7fffee77d010). what's going on here?
finally, if step forward, get:
program received signal sigsegv, segmentation fault. 0x00007ffff7a0dd50 in cv::mat::copyto(cv::_outputarray const&) const () /usr/local/lib/libopencv_core.so.2.4
opencv tries access first element clone it, , fails.
why first element accessible in constructor, not accessible in exhaustive_comparison
member function?
exhaustivecomparisonmt(vector<mat> sigs, mat dist) { m_sigs = &sigs; m_dist = &dist; // gdb breakpoint 1 here }
you passing variables sigs
, dist
copy - meaning, lifetime limited function.
if try address , use afterwards, going point unknown data - because upon leaving function, these objects destroyed , addresses used else later on.
if want have address of objects passed parameters, should use references - way, retrieve address of original objects. work assuming, of course, you're passing lvalues functions , not destroyed in meantime.
exhaustivecomparisonmt(vector<mat> &sigs, mat &dist) { m_sigs = &sigs; m_dist = &dist; // gdb breakpoint 1 here }
Comments
Post a Comment