C++ 删除导致访问冲突的std::list

C++ 删除导致访问冲突的std::list,c++,C++,对于一个学校项目,我的小组正在使用OpenCV来捕获视频。从这些(自上而下)图像中,提取对象的位置并将其转换为点列表。然后使用(克服可能的非凸对象问题)对这些点进行三角剖分。然后,使用三角形地面窗格的坐标,我们使用freeglut在3D中绘制对象。(侧窗格和顶窗格使用地面窗格坐标计算)。问题是,当我们删除旧的点列表时,应用程序会随机崩溃。有时在1秒后,有时在30秒后,有时在几分钟后。我们得到的错误是“访问冲突写入位置0xCCCC” 我们的守则: void WorldLayoutBuilder::

对于一个学校项目,我的小组正在使用OpenCV来捕获视频。从这些(自上而下)图像中,提取对象的位置并将其转换为点列表。然后使用(克服可能的非凸对象问题)对这些点进行三角剖分。然后,使用三角形地面窗格的坐标,我们使用freeglut在3D中绘制对象。(侧窗格和顶窗格使用地面窗格坐标计算)。问题是,当我们删除旧的点列表时,应用程序会随机崩溃。有时在1秒后,有时在30秒后,有时在几分钟后。我们得到的错误是“访问冲突写入位置0xCCCC”

我们的守则:

void WorldLayoutBuilder::update()
{
pointList.clear();

// Capture image
<code to capture image and get countours>

for(size_t i = 0; i < contours.size(); i++)
{
    if(contours[i].size() > 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<Point> newObject;

        for(size_t j = 0; j < 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(&pointList);
contours.clear();

<code to release images, etc>
}
void WorldLayoutBuilder::update()
{
pointList.clear();
//捕获图像
<捕获图像并获取计数的代码>
对于(size_t i=0;i50)
{
近似多边形(等高线[i],近似形状,cv::弧长(cv::Mat(等高线[i]),真)*0.04,真);
绘制等高线(绘制,等高线,i,cv::标量(255,0,0),0);
std::矢量新对象;
对于(size_t j=0;j<轮廓[i]。size();j++)
{
cv::Point newPoint=等高线[i][j];
newObject.push_back(点((float)newPoint.x/100,0.0f,(float)newPoint.y/100));
}
点列表。推回(newObject);
}
}
ObjectCreator3D::createObjects(&pointList);
轮廓。清晰();
<发布图像等的代码>
}
这将捕获图像,检索对象的坐标,然后调用ObjectCreator3D::createObjects():

void ObjectCreator3D::createObjects(std::list*inputList)
{
std::list*tempObjects=new std::list;
对于(标准::向量和点对象:*输入列表)
{                       
WorldObject世界对象(&pointObject);
tempObjects->push_back(世界对象);
}
DataStorage::getInstance()->setObjects(tempObjects);
}
所有对象都将转换为世界对象:

#include <list>
#include <iostream>
#include <GL/glut.h>
#include <GL/freeglut.h>
#include <time.h>

#include "WorldObject.h"
#include "Point.h"

//Constant height - adjustable/randomized solution is partially implemented in the     constructor.
const float WorldObject::HEIGHT = 5.0f;

template <class C> void FreeClear(C & cntr)
{
for(typename C::iterator it = cntr.begin(); it != cntr.end(); ++it)
{
    delete * it;
}
cntr.clear();
}

WorldObject::WorldObject(std::vector<Point>* 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 &point : *pointList) 
    //point.setY(fGeneratedY);
    point.setY(HEIGHT);

this->pointList = pointList;
}

WorldObject::~WorldObject()
{
//Cleanup
delete cdt;
FreeClear(polyPoints);
}

/*
Author Tim Cocu & 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 < calculatedTriangles.size(); i++)
{
    p2t::Triangle& t = *calculatedTriangles[i];
    p2t::Point& a = *t.GetPoint(0);
    p2t::Point& b = *t.GetPoint(1);
    p2t::Point& 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 < calculatedTriangles.size(); i++)
{
    p2t::Triangle& t = *calculatedTriangles[i];
    p2t::Point& a = *t.GetPoint(0);
    p2t::Point& b = *t.GetPoint(1);
    p2t::Point& 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 < pointList->size(); iPaneCounter++)
{
    Point firstPoint = (*pointList)[iPaneCounter];
    Point secondPoint (0.0f, 0.0f, 0.0f);

    if(iPaneCounter + 1 < pointList->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() > 0)
    FreeClear(polyPoints);

//Convert our Points to p2t::Points
for(std::size_t iBottomIndex = 0; iBottomIndex < pointList->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->Triangulate();

//Set the triangles to use for drawing
calculatedTriangles = cdt->GetTriangles();
}

/*
Retrieve a pointer to a list of Points
*/
std::vector<Point>* WorldObject::getPoints()
{
return pointList;
}

/*
Retrieve a pointer to a list of p2t::Triangles
*/
std::vector<p2t::Triangle*> WorldObject::getCalculatedTriangles()
{
return calculatedTriangles;
}
#包括
#包括
#包括
#包括
#包括
#包括“WorldObject.h”
#包括“h点”
//恒定高度-可调/随机化解决方案在构造函数中部分实现。
常量浮动世界对象::高度=5.0f;
模板无效清除(C&cntr)
{
对于(typename C::iterator it=cntr.begin();it!=cntr.end();++it)
{
删除*它;
}
cntr.clear();
}
WorldObject::WorldObject(标准::向量*点列表)
{
//TODO,当我们有时间的时候。看起来很难,因为每次更新高度都会改变。。。
/*srand(时间(空));
float fGeneratedY=(rand()%20+2)/2.0f*/
cdt=空PTR;
对于(点和点:*点列表)
//点。setY(fGeneratedY);
赛提角(高度);
此->点列表=点列表;
}
WorldObject::~WorldObject()
{
//清理
删除cdt;
FreeClear(多点);
}
/*
作者Tim Cocu和Bas Rops
用于绘制世界对象的函数
*/
void WorldObject::draw()
{
glPushMatrix();
GL3F(0.8f,0.8f,0.8f);
//计算我们的底部窗格
计算角度();
//底部窗格
for(无符号整数i=0;isize();iPaneCounter++)
{
Point firstPoint=(*pointList)[IPanCounter];
第二点(0.0f,0.0f,0.0f);
如果(iPanCounter+1size())
secondPoint.set((*pointList)[iPaneCounter+1].getX(),(*pointList)[iPaneCounter+1].getY(),(*pointList)[iPaneCounter+1].getZ());
其他的
secondPoint.set((*pointList)[0].getX(),(*pointList)[0].getY(),(*pointList)[0].getZ());
glBegin(GL_多边形);
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());
格伦德();
}
}
/*
计算构成地面或顶部窗格的三角形。用于计算可能的非凸对象
*/
void WorldObject::calculateTriangles()
{
//清空多点列表
如果(多点.size()>0)
FreeClear(多点);
//将我们的点转换为p2t::点
对于(std::size\u t iBottomIndex=0;iBottomIndexsize();iBottomIndex++)
polyPoints.push_back(新的p2t::Point((*pointList)[iBottomIndex].getX(),(*pointList)[iBottomIndex].getZ());
if(cdt==nullptr)
//创建CDT(约束Delaunay三角剖分)并添加主多点
//注意:多点必须是简单多边形。多点的点构成约束边。不允许重复点!
cdt=新p2t::cdt(多点);
//把我们的多点交上来
#include <list>
#include <iostream>
#include <GL/glut.h>
#include <GL/freeglut.h>
#include <time.h>

#include "WorldObject.h"
#include "Point.h"

//Constant height - adjustable/randomized solution is partially implemented in the     constructor.
const float WorldObject::HEIGHT = 5.0f;

template <class C> void FreeClear(C & cntr)
{
for(typename C::iterator it = cntr.begin(); it != cntr.end(); ++it)
{
    delete * it;
}
cntr.clear();
}

WorldObject::WorldObject(std::vector<Point>* 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 &point : *pointList) 
    //point.setY(fGeneratedY);
    point.setY(HEIGHT);

this->pointList = pointList;
}

WorldObject::~WorldObject()
{
//Cleanup
delete cdt;
FreeClear(polyPoints);
}

/*
Author Tim Cocu & 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 < calculatedTriangles.size(); i++)
{
    p2t::Triangle& t = *calculatedTriangles[i];
    p2t::Point& a = *t.GetPoint(0);
    p2t::Point& b = *t.GetPoint(1);
    p2t::Point& 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 < calculatedTriangles.size(); i++)
{
    p2t::Triangle& t = *calculatedTriangles[i];
    p2t::Point& a = *t.GetPoint(0);
    p2t::Point& b = *t.GetPoint(1);
    p2t::Point& 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 < pointList->size(); iPaneCounter++)
{
    Point firstPoint = (*pointList)[iPaneCounter];
    Point secondPoint (0.0f, 0.0f, 0.0f);

    if(iPaneCounter + 1 < pointList->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() > 0)
    FreeClear(polyPoints);

//Convert our Points to p2t::Points
for(std::size_t iBottomIndex = 0; iBottomIndex < pointList->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->Triangulate();

//Set the triangles to use for drawing
calculatedTriangles = cdt->GetTriangles();
}

/*
Retrieve a pointer to a list of Points
*/
std::vector<Point>* WorldObject::getPoints()
{
return pointList;
}

/*
Retrieve a pointer to a list of p2t::Triangles
*/
std::vector<p2t::Triangle*> WorldObject::getCalculatedTriangles()
{
return calculatedTriangles;
}
void DataStorage::setObjects(std::list<WorldObject>* objectList)
{
delete this->objectList;
this->objectList = objectList;
}
for(std::vector<Point>&pointObject : *inputList)
{                       
  WorldObject worldObject(&pointObject);
  tempObjects->push_back(worldObject);
}
//Default Constructor
WorldObject::WorldObject(std::vector<Point>* pointList)
{
  float fGeneratedY = (rand() % 20 + 2) / 2.0f;*/

  cdt = nullptr;

  for (Point &point : *pointList) 
    point.setY(HEIGHT);

  this->pointList = pointList;
}