Note that there are some explanatory texts on larger screens.

plurals
  1. PODeletion of std::list causing Access violation
    primarykey
    data
    text
    <p>For a school project, my group is using OpenCV to capture video. From these (top-down) images, positions of objects are extracted and turned into a list of Points. Those Points then get triangulated using <a href="http://code.google.com/p/poly2tri/" rel="nofollow">http://code.google.com/p/poly2tri/</a> (to overcome the problem of possible non-convex objects). Then, using the coordinates of the triangulated ground pane, we draw the objects in 3D using freeglut. (Side and Top panes are calculated using the ground pane coordinates). The problem we have is that when we delete our old list of Points, the application randomly crashes. Sometimes after 1 second, sometimes after 30 seconds, sometimes after a few minutes. The error we get is "Access violation writing location 0xCCCCCCCC"</p> <p>Our code:</p> <pre><code>void WorldLayoutBuilder::update() { pointList.clear(); // Capture image &lt;code to capture image and get countours&gt; for(size_t i = 0; i &lt; contours.size(); i++) { if(contours[i].size() &gt; 50) { approxPolyDP(contours[i], approxShape, cv::arcLength(cv::Mat(contours[i]), true)*0.04, true); drawContours(drawing, contours, i, cv::Scalar(255, 0, 0), 0); std::vector&lt;Point&gt; newObject; for(size_t j = 0; j &lt; contours[i].size(); j++) { cv::Point newPoint = contours[i][j]; newObject.push_back(Point((float) newPoint.x / 100, 0.0f,(float) newPoint.y / 100)); } pointList.push_back(newObject); } } ObjectCreator3D::createObjects(&amp;pointList); contours.clear(); &lt;code to release images, etc&gt; } </code></pre> <p>This captures an image, retrieves coordinates of objects, and then calls ObjectCreator3D::createObjects():</p> <pre><code>void ObjectCreator3D::createObjects(std::list&lt;std::vector&lt;Point&gt;&gt;* inputList) { std::list&lt;WorldObject&gt;* tempObjects = new std::list&lt;WorldObject&gt;; for(std::vector&lt;Point&gt;&amp;pointObject : *inputList) { WorldObject worldObject(&amp;pointObject); tempObjects-&gt;push_back(worldObject); } DataStorage::getInstance()-&gt;setObjects(tempObjects); } </code></pre> <p>All objects are turned into WorldObjects:</p> <pre><code>#include &lt;list&gt; #include &lt;iostream&gt; #include &lt;GL/glut.h&gt; #include &lt;GL/freeglut.h&gt; #include &lt;time.h&gt; #include "WorldObject.h" #include "Point.h" //Constant height - adjustable/randomized solution is partially implemented in the constructor. const float WorldObject::HEIGHT = 5.0f; template &lt;class C&gt; void FreeClear(C &amp; cntr) { for(typename C::iterator it = cntr.begin(); it != cntr.end(); ++it) { delete * it; } cntr.clear(); } WorldObject::WorldObject(std::vector&lt;Point&gt;* pointList) { //TODO, when we have time. Seems difficult because height will change each update... /*srand (time(NULL)); float fGeneratedY = (rand() % 20 + 2) / 2.0f;*/ cdt = nullptr; for (Point &amp;point : *pointList) //point.setY(fGeneratedY); point.setY(HEIGHT); this-&gt;pointList = pointList; } WorldObject::~WorldObject() { //Cleanup delete cdt; FreeClear(polyPoints); } /* Author Tim Cocu &amp; Bas Rops Function for drawing the WorldObject */ void WorldObject::draw() { glPushMatrix(); glColor3f(0.8f, 0.8f, 0.8f); //Calculate our bottom pane calculateTriangles(); //BOTTOM PANE for (unsigned int i = 0; i &lt; calculatedTriangles.size(); i++) { p2t::Triangle&amp; t = *calculatedTriangles[i]; p2t::Point&amp; a = *t.GetPoint(0); p2t::Point&amp; b = *t.GetPoint(1); p2t::Point&amp; c = *t.GetPoint(2); glBegin(GL_TRIANGLES); glNormal3f(0, -1, 0); glVertex3f((GLfloat)a.x, (GLfloat)0.0f, (GLfloat)a.y); glVertex3f((GLfloat)b.x, (GLfloat)0.0f, (GLfloat)b.y); glVertex3f((GLfloat)c.x, (GLfloat)0.0f, (GLfloat)c.y); glEnd(); } //TOP PANE for (unsigned int i = 0; i &lt; calculatedTriangles.size(); i++) { p2t::Triangle&amp; t = *calculatedTriangles[i]; p2t::Point&amp; a = *t.GetPoint(0); p2t::Point&amp; b = *t.GetPoint(1); p2t::Point&amp; c = *t.GetPoint(2); glBegin(GL_TRIANGLES); glNormal3f(0, 1, 0); glVertex3f((GLfloat)a.x, (GLfloat)HEIGHT, (GLfloat)a.y); glVertex3f((GLfloat)b.x, (GLfloat)HEIGHT, (GLfloat)b.y); glVertex3f((GLfloat)c.x, (GLfloat)HEIGHT, (GLfloat)c.y); glEnd(); } glColor3f(1.0f, 1.0f, 1.0f); //SIDE PANES for(std::size_t iPaneCounter = 0; iPaneCounter &lt; pointList-&gt;size(); iPaneCounter++) { Point firstPoint = (*pointList)[iPaneCounter]; Point secondPoint (0.0f, 0.0f, 0.0f); if(iPaneCounter + 1 &lt; pointList-&gt;size()) secondPoint.set((*pointList)[iPaneCounter + 1].getX(), (*pointList)[iPaneCounter + 1].getY(), (*pointList)[iPaneCounter + 1].getZ() ); else secondPoint.set((*pointList)[0].getX(), (*pointList)[0].getY(), (*pointList)[0].getZ()); glBegin(GL_POLYGON); float fNormalX = (firstPoint.getY() * secondPoint.getZ()) - (firstPoint.getZ() * secondPoint.getY()); float fNormalY = -((secondPoint.getZ() * firstPoint.getX()) - (secondPoint.getX() * firstPoint.getZ())); float fNormalZ = (firstPoint.getX() * secondPoint.getY()) - (firstPoint.getY() * secondPoint.getX()); glNormal3f(fNormalX, fNormalY, fNormalZ); glVertex3f(firstPoint.getX(), 0.0f, firstPoint.getZ()); glVertex3f(secondPoint.getX(), 0.0f, secondPoint.getZ()); glVertex3f(secondPoint.getX(), secondPoint.getY(), secondPoint.getZ()); glVertex3f(firstPoint.getX(), firstPoint.getY(), firstPoint.getZ()); glEnd(); } } /* Calculates triangles that make a ground or top pane. Used for calculating possible non-convex objects */ void WorldObject::calculateTriangles() { //Empty the polyPoints list if(polyPoints.size() &gt; 0) FreeClear(polyPoints); //Convert our Points to p2t::Points for(std::size_t iBottomIndex = 0; iBottomIndex &lt; pointList-&gt;size(); iBottomIndex++) polyPoints.push_back(new p2t::Point((*pointList)[iBottomIndex].getX(), (*pointList)[iBottomIndex].getZ())); if(cdt == nullptr) //Create CDT (Constrained Delaunay Triangulation) and add primary polyPoints //NOTE: polyPoints must be a simple polygon. The polyPoints' points constitute constrained edges. No repeating points are allowed! cdt = new p2t::CDT(polyPoints); //Turn our polyPoints into p2t::Triangles cdt-&gt;Triangulate(); //Set the triangles to use for drawing calculatedTriangles = cdt-&gt;GetTriangles(); } /* Retrieve a pointer to a list of Points */ std::vector&lt;Point&gt;* WorldObject::getPoints() { return pointList; } /* Retrieve a pointer to a list of p2t::Triangles */ std::vector&lt;p2t::Triangle*&gt; WorldObject::getCalculatedTriangles() { return calculatedTriangles; } </code></pre> <p>When all WorldObjects are created, they are stored in DataStorage, DataStorage::getInstance()->setObjects() is called:</p> <pre><code>void DataStorage::setObjects(std::list&lt;WorldObject&gt;* objectList) { delete this-&gt;objectList; this-&gt;objectList = objectList; } </code></pre> <p>The application seems to crash on delete this->objectList; in setObjects(), so we think the application is trying to delete things he can't delete.</p> <p>Any help would be greatly appreciated, we've been on this for a few days already</p>
    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.
 

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