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

标签 c++

对于一个学校项目,我的团队正在使用 OpenCV 来捕捉视频。从这些(自上而下的)图像中,对象的位置被提取出来并变成一个点列表。然后使用 http://code.google.com/p/poly2tri/ 对这些点进行三角测量(以克服可能的非凸物体的问题)。然后,使用三角化地面 Pane 的坐标,我们使用 freeglut 在 3D 中绘制对象。 (侧 Pane 和顶 Pane 是使用地面 Pane 坐标计算的)。我们遇到的问题是,当我们删除旧的点列表时,应用程序会随机崩溃。有时 1 秒后,有时 30 秒后,有时几分钟后。我们得到的错误是“Access violation writing location 0xCCCCCCCC”

我们的代码:

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>
}

这会捕获图像,检索对象的坐标,然后调用 ObjectCreator3D::createObjects():

void ObjectCreator3D::createObjects(std::list<std::vector<Point>>* inputList)
{
std::list<WorldObject>* tempObjects = new std::list<WorldObject>;

for(std::vector<Point>&pointObject : *inputList)
{                       
    WorldObject worldObject(&pointObject);
    tempObjects->push_back(worldObject);
}

DataStorage::getInstance()->setObjects(tempObjects);
}

所有物体都变成了WorldObjects:

#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;
}

当所有的WorldObjects被创建后,它们被存储在DataStorage中,DataStorage::getInstance()->setObjects()被调用:

void DataStorage::setObjects(std::list<WorldObject>* objectList)
{
delete this->objectList;
this->objectList = objectList;
}

应用程序似乎在删除 this->objectList 时崩溃;在 setObjects() 中,因此我们认为应用程序正在尝试删除他无法删除的内容。

任何帮助将不胜感激,我们已经研究了几天

最佳答案

在这里,您将指向列表拥有的对象的指针传递给 WorldObject 的构造函数:

for(std::vector<Point>&pointObject : *inputList)
{                       
  WorldObject worldObject(&pointObject);
  tempObjects->push_back(worldObject);
}

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;
}

这意味着 WorldObject::pointList仅在 std::list 时有效你构建了你的WorldObject s from 还在附近。 (在那之后,结果是不确定的——它可能会工作,它可能会崩溃,它可能会格式化你的硬盘驱动器并将你的身份泄露给德克萨斯州)。

如果您坚持使用原始指针,作为程序员,您有责任检查和跟踪每个指针的生命周期。这很容易出错,并且会导致随机崩溃,您会发现难以追踪。

停止使用原始指针。相反,如果对象拥有资源,则将其存储在 std::unique_ptr<> 中.如果你想让同一个资源被多个对象共享,使用std::shared_ptrstd::weak_ptr ,除非这些对象中的一个以外的所有对象的生命周期都比其他对象短得多,而且可以保证。

关于c++ - 删除导致访问冲突的 std::list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16788951/

相关文章:

c++ - 为什么我不能有一个纯虚拟赋值运算符?

c++ - 使用 Berkeley 套接字进行多路复用

c++ - 缺少未使用的纯虚拟方法的实现时链接失败

c++ - 使用来自控制台的输入来分割列表的故障初始化 vector

c++ - 无法将 std::bind 的返回值转换为 void 函数指针

c++ - C++ 的 Visual Studio ?

c++ - 在继承层次结构中复制和 move

C++:std::istream 检查 EOF 而不读取/消耗 token /使用运算符>>

c# - 将内核链接在一起时值随机变化

c++ - 具有模板构造函数特化的模板类,用于初始化模板化基类