Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9010767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:33:02+00:00 2026-06-16T02:33:02+00:00

my problem is that a std:vector<vec4i> ( p_lines and filt_lines) are created in a

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:33:03+00:00Added an answer on June 16, 2026 at 2:33 am

    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++)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple question, how does one create a function which takes an unsigned char std::vector
I'm trying to create a generic function that removes duplicates from an std::vector. Since
I would like to initialize boost::random::discrete_distribution with an std::vector<double> . My problem is that
I've been trying to find a really function that let me convert from std::vector
Simple problem that I can't figure out... How can I print a '%' character
I'm facing a problem that I can summarize as it follows: I have a
I'm trying to design a class which has two vectors of large sequences. std::vector<double>
I am developing Python bindings for a C++ class which extends a std::vector instantiation.
I have an ANTLR rule that would return a vector: main returns [std::vector<int> v]
Today I ran into the problem that accessing the vector elements slowed down with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.