Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用vector::擦除导致MalCube():内存损坏(FAST)C++_C++_Vector_Allocation_Erase - Fatal编程技术网

使用vector::擦除导致MalCube():内存损坏(FAST)C++

使用vector::擦除导致MalCube():内存损坏(FAST)C++,c++,vector,allocation,erase,C++,Vector,Allocation,Erase,我有一个组类,它有一个向量v_组; 和ofc获取向量的大小并删除一项: 小组课 void Group::drawGroup() { for(int i = 0; i < v_group.size(); i++) { v_group.at(i).draw(); } } void Group::add(Mesh object) { v_group.push_back(object); } Mesh &Group::get(int loc

我有一个组类,它有一个向量v_组; 和ofc获取向量的大小并删除一项:

小组课

void Group::drawGroup()
{
    for(int i = 0; i < v_group.size(); i++)
    {
       v_group.at(i).draw();
    }
}

void Group::add(Mesh object)
{
    v_group.push_back(object);
}

Mesh &Group::get(int location)
{
    return v_group.at(location);
}

void Group::clear()
{
    v_group.clear();
}

void Group::remove(int location)
{
    v_group.erase(v_group.begin()+(location));
}

bool Group::remove(Mesh object)
{
    return true;
}

int Group::size()
{
   return v_group.size();
}
在其他地方,我将使用以下命令从初始组向量中删除对象:

DropObject::DropObject(Renderer *renderer)
{
    this->renderer = renderer;

    //insert a i (count) into the vector to represent the already Available objects, and continue from there with the DropObjects
    for(int i = 0;i < renderer->getGroupSize();i++)
    {
        yIndex.push_back(10);      //Vector 
        xIndex.push_back(10);      //Vector
    }

}

DropObject::~DropObject()
{

}

void DropObject::Create(int &ENEMY_movePosition, const int &ENEMY_start_height)
{
    renderer->addObject(20,20,"");
    xIndex.push_back(ENEMY_movePosition);
    yIndex.push_back(ENEMY_start_height);//just add index, nothing else cam into my mind right now ._.
    cout << ENEMY_start_height << endl;

    for(int i = 0; i < yIndex.size();i++)
    {
        cout << "y vec= " << yIndex[i] << endl;
    }

}

void DropObject::Move(int index)
{
    //current index set to isRemoved = false , because it's still there
    isRemoved = false;

    //x remains the same, y decrease by 1 (->  =-1)
  try
  {
    yIndex[index] -= 2;
    renderer->move(index,xIndex[index],yIndex[index]);

    if(yIndex[index] < -250)
    {
        //renderer->RemoveObject(index);
        renderer->GetGroup().remove(index);
        yIndex.erase(yIndex.begin() + index);
        xIndex.erase(yIndex.begin() + index);

        isRemoved = true;
    }
   }catch(std::out_of_range ex)
    {
        cout << "DropObject move out of range:" << ex.what() << endl;
    }
}
它可以工作,但是在xIndex.eraseyIndex.begin+索引之后;程序因分段错误而崩溃 或者当我再次尝试从组中调用向量时。我得到一个malloc:内存损坏快速错误

也许在删除一个元素后,内存仍然分配给它,因此我认为这样的错误

有人能帮忙吗

    yIndex.erase(yIndex.begin() + index);
    xIndex.erase(yIndex.begin() + index);
在xIndex.erase中,我使用yIndex而不是xIndex。
可怕的错误。

如果您可以创建一个来显示给我们,也许可以在组中的vector第二次调用后添加到您的代码中。也许为了简单起见,您在删除之前没有添加任何位置检查,因此您可能正在删除导致此问题的越界条目。如果不是这样的话,你能修改你的问题吗?当我再次尝试从组中调用向量时,你的意思是什么?求你了!如果您正在与新成员和指针成员混在一起,那么您可能忘记了。如果出于某种原因确实需要动态分配,请将m_group的类型更改为group,或std::unique_ptr。
    yIndex.erase(yIndex.begin() + index);
    xIndex.erase(yIndex.begin() + index);