Question

my problem is that a std:vector<vec4i> ( p_lines and filt_lines) are created in a function which can not be deallocate at the end of the function. So i get an free(): invalid pointer error while running the code. In some cases i go segmentation fault error, but i couldt trace the source with gdb. The aim of the function is to find the edges on a picture and there intersection point, in the region where the most lines are intersecting it is drown a rectangle. The pictures are coming from a ROS video feed.

void Probabilistic_Hough( Mat src, Mat &result )
{
    result=src;
    cvtColor( src, src, CV_RGB2GRAY );
    Canny( src, src, 50, 200, 3 );
    HoughLinesP( src, p_lines, 1, CV_PI/180, 85, 40, 10 );  
vector<Vec4i> filt_lines;
        int fls=filt_lines.size();
        int pls=p_lines.size();
if (!p_lines.empty()){
    for(size_t i = 0; i < p_lines.size();i++)
    {
        Vec4i l = p_lines[i];
        if ((l[0]<l[2]-10 ||  l[0]>l[2]+10) &&  (l[1]<l[3]-10 || l[1]>l[3]+10))
        {
            line( result/*probabilistic_hough*/, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(255,0,0), 2);
            filt_lines.push_back(l);

        }

    }


    fls=filt_lines.size();
    pls=p_lines.size();
    printf("f=%d p=%d",fls,pls);
    if (filt_lines.size()>1 && !filt_lines.empty()) 
    {
        int sizeX=src.rows/11, sizeY=src.cols/11;
        int imgGrid[11][11];

        for (int i=0;i<11;i++)
        {
            for (int j=0;j<11;j++) imgGrid[i][j]=0;
        }


        for (size_t i = 0;i<filt_lines.size()-1;i++)
        {           
            Point2f o1 (filt_lines[i][0], filt_lines[i][1]) ;
            Point2f p1 (filt_lines[i][2], filt_lines[i][3]) ; 
            for (size_t j=i+1;j<filt_lines.size();j++)
            {
                Point2f o2 (filt_lines[j][0], filt_lines[j][1]) ;
                Point2f p2 (filt_lines[j][2], filt_lines[j][3]) ;
                Point2f x = o2 - o1;
                Point2f d1 = p1 - o1;
                Point2f d2 = p2 - o2;
                float cross = d1.x*d2.y - d1.y*d2.x;
                if (abs(cross) > 1e-8)
                {
                    double t1 = (x.x * d2.y - x.y * d2.x)/cross;
                    Point2f crosP  = o1 + d1 * t1;
                    if (crosP.x>0.0 && crosP.y>0.0 && crosP.y< src.rows && crosP.x<src.cols)
                    imgGrid[int(crosP.x/sizeX)][int(crosP.y/sizeY)]++;
                }
            }
        }
        int countP=0,maxi=0, maxj=0;
        for (int i=0;i<11;i++)
        {
            for (int j=0;j<11;j++)
            {

            if (countP<imgGrid[i][j])
                {
                    countP=imgGrid[i][j];
                    maxi=i;
                    maxj=j;
                }
            }
        }
        printf("c=%d \n",countP);
        if (countP>0)
        {
            Point vanishP1 = cvPoint(maxi*sizeX,maxj*sizeY), vanishP2 = cvPoint((maxi+1)*sizeX,(maxj+1)*sizeY);
            CvScalar red= CV_RGB(220,0,0);
            if (vanishP1.x>=0 && vanishP1.y>=0 && vanishP2.x>0 && vanishP2.y>0)             
            rectangle(src,vanishP1,vanishP2,red,2,8,0);     
            result=src;
        } else {printf("only 1 intersection, out of img"); }
    } else 
    {
    printf("No filtered lines");
    }


} else 
    {       
    printf("no lines on image");
    }

}

I tired to debug with gdb and the invalid pointer address is the beginning address of the p_lines vector. The gdb frame points to the las } of the function. How can I solve the free(): invalid pointer error and what could be the problem that cause segmentation fault. The error msg from gdb:

    glibc detected *** /home/xyz: free(): invalid pointer: 0x080cc482 ***
    ======= Backtrace: =========
    /lib/tls/i686/cmov/libc.so.6(+0x6b591)[0x12fe591]
    /lib/tls/i686/cmov/libc.so.6(+0x6cde8)[0x12ffde8]
    /lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0x1302ecd]
    /usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0x11fa741]
    /home/xyz(_Z19Probabilistic_HoughN2cv3MatERS0_+0x8c5)[0x804b3b5]
    /home/xyz(_Z13imageCallbackRKN5boost10shared_ptrIKN11sensor_msgs6Image_ISaIvEEEEE+0x5c8)[0x804b9a0][0x804b3b5]
/home/xyz(_Z13imageCallbackRKN5boost10shared_ptrIKN11sensor_msgs6Image_ISaIvEEEEE+0x5c8)[0x804b9a0]

and the print of p_lines is(_M_start = 0x80cc482 is equal with free(): i. p. :0x080cc482):

(gdb) p p_lines
$1 = {<std::_Vector_base<cv::Vec<int, 4>, std::allocator<cv::Vec<int, 4> > >> = {
    _M_impl = {<std::allocator<cv::Vec<int, 4> >> = {<__gnu_cxx::new_allocator<cv::Vec<int, 4> >> = {<No data fields>}, <No data fields>}, _M_start = 0x80cc482, _M_finish = 0x80cc540, 
      _M_end_of_storage = 0x80cc540}}, <No data fields>}

I remade some test after correcting my typo and added an if (countP>0) at the end. Found the frame where the invalid pointer operation appears, img has 12 p_lines and 2 filt_lines, however countP=0 (so the two line intersects out of the img size). The gdb showed that after calling HoughLinesP() the starting addres of p_lines is _M_start = 0x80d4e70, after the crash the the free(): i.p. appeared at 0x080d4e71 and in my functions gdb frame the staring address of p_lines is _M_start = 0x80d4e71. What the frack? How is it possible that the starting addres of the vector changes with 1 bit?

Finally I found the exact moment when the bit change in the p_lines happens:

(gdb) next
    98                      imgGrid[int(crosP.x/sizeX)][int(crosP.y/sizeY)]++;
    (gdb) p p_lines 
    $68 = {<std::_Vector_base<cv::Vec<int, 4>, std::allocator<cv::Vec<int, 4> > >> = {
        _M_impl = {<std::allocator<cv::Vec<int, 4> >> = {<__gnu_cxx::new_allocator<cv::Vec<int, 4> >> = {<No data fields>}, <No data fields>}, 
          _M_start = 0x80d4c78, _M_finish = 0x80d4d38, _M_end_of_storage = 0x80d4d38}}, <No data fields>}
    (gdb) next
    84              for (size_t j=i+1;j<filt_lines.size();j++)
    (gdb) p p_lines 
    $69 = {<std::_Vector_base<cv::Vec<int, 4>, std::allocator<cv::Vec<int, 4> > >> = {
        _M_impl = {<std::allocator<cv::Vec<int, 4> >> = {<__gnu_cxx::new_allocator<cv::Vec<int, 4> >> = {<No data fields>}, <No data fields>}, 
          _M_start = 0x80d4c79, _M_finish = 0x80d4d38, _M_end_of_storage = 0x80d4d38}}, <No data fields>}
(gdb) p j
$71 = 1

So in this case the size of filt_lines is 2. and and the bit shift appears after the 1st line and 2nd are checked and the jumps back to the for when j=1 and j<2. Why? :/ Pleas help me out, solving this error. thx

Was it helpful?

Solution

You have index overrun on filt_lines vector in innermost loop. I suppose it should be

 for (size_t j = i + 1; j < filt_lines.size(); j++)

instead of

 for (size_t j=i+1;j<p_lines.size();j++)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top