Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your new RoadLine class will certainly lead to disaster :</p> <pre><code>RoadLine::RoadLine(CvPoint* aLine, int aLane){ line = aLine; lane = aLane; } RoadLine::RoadLine(const RoadLine &amp; myRoadLine){ line = myRoadLine.line; lane = 1; } RoadLine::~RoadLine(){ delete line; } </code></pre> <p>code using it :</p> <pre><code> if(lineCount == a){ cvLine(img_final, roadLine[0], roadLine[1], CV_RGB(0,0,255), 3, CV_AA, 0 ); RoadLine myLine = RoadLine(roadLine, 1);//create object on the Stack roadVector-&gt;push_back(myLine); //Push COPY of myLine cvShowImage("Plate Detection", img_final); cvWaitKey(0); }//Stack-based object "myLine" is automatically destroyed here (leaves scope) </code></pre> <p>the automatic destruction of "myLine" will delete "myLine.line" (in RoadLine's dtor) but "myLine.line" is still referenced in the vector (you just pushed it).</p> <p>You have to either make a DEEP COPY of line (as others suggested), something like this :</p> <pre><code>RoadLine::RoadLine(const RoadLine &amp; myRoadLine){ line = new CvPoint(*myRoadLine.line);//assuming CvPoint can be copy-constructed lane = 1; } </code></pre> <p>Or use a CvLine object rather than a pointer (or something else, need more context)</p> <p>EDIT : Dirk Gently's copy-ctorhas a bug, because it leaks memory to the former "line"-member should be :</p> <pre><code>RoadLine&amp; operator=(const RoadLine &amp; o){ if (this != &amp;o) { //Remember to check for self-assignment. delete []line;//delete[] vs. delete ! line = 0;//if next line throws at least we won't double-delete line line = new CvPoint[ 2 ]; //this might throw ! should catch (or redesign to get rid of new (prefered) line[ 0 ] = o.line[ 0 ]; line[ 1 ] = o.line[ 1 ]; lane = o.lane; } return *this; } //consistent constructor ! RoadLine::RoadLine(CvPoint* aLine, int aLane) :line(new CvPoint[2]),//might throw, but its better to throw in initializer ! (if you just have one pointer it might be ok to do it like this) lane(aLane) { line[0] = aLine[0]; line[1] = aLine[1]; } RoadLine::~RoadLine(){ delete[] line;//also use delete[] vs. normal delete here ! } </code></pre> <p>EDIT 2 : I almost forgot that I had an idea why it crashes ! maybe you try to build a pair with last and last+1 CvPoint (like this obviously false code)?</p> <pre><code>CvPoint Pnts[2] = {CvPoint(0,0),CvPoint(1,1)}; Roadline Line(&amp;Pnts[1],1);//tries to access Pnts[2] which is one past end ! </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload