简体   繁体   中英

using vector::erase causes a malloc(): memory corruption (fast) c++

I have a Group class which has a vector v_group; and ofc something to get the size of the vector and to remove a item:

Group class

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

In my other class I'm creating a Group object:

in header: `Group *m_group;`

Renderer::Renderer() : m_group(new Group())
{
}

void Renderer::drawGL()
{
   m_group->drawGroup();
}

void Renderer::addObject(float width,float height,string texture_url)
{
    UseableObject uobj(width,height,texture_url);
    m_group->add(uobj);
}

void Renderer::RemoveObject(int index)
{
    m_group->remove(index);
}

void Renderer::move(int objectIndex, float x, float y)
{
    m_group->get(objectIndex).setPosX(x);
    m_group->get(objectIndex).setPosY(y);
}

Group& Renderer::GetGroup()
{
    return *m_group;
}

Somewhere else I'll want to erase a object from the initial Group vector using:

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

which works, but after xIndex.erase(yIndex.begin() + index); the programm crashes with a segmentation fault or when I'm trying to call the vector from Group again. I get a malloc(): memory corruption (fast) error.

Maybe after erasing a element, the memory is still allocated for it, and therefore i'g such errors?

anyone can help?

    yIndex.erase(yIndex.begin() + index);
    xIndex.erase(yIndex.begin() + index);

at xIndex.erase() I used yIndex and not xIndex. Terrible mistake.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM