0

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?

jeromintus
  • 367
  • 1
  • 5
  • 14
  • Maybe, if you can create a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) to show us instead? – Some programmer dude Aug 08 '14 at 11:25
  • Add to your code in post second call of vector from Group. – Dakorn Aug 08 '14 at 11:31
  • Maybe for simplicity, you didn't add any check of the location before erasing, so you might be erasing an out of bound entry which causes this. Could you adapt your question if this is not the case? – stefaanv Aug 08 '14 at 11:44
  • What do you mean by "when I'm trying to call the vector from Group again"? sscce please! – Werner Henze Aug 08 '14 at 11:52
  • If you're messing around with `new` and pointer members, then you probably forgot the [Rule of Three](http://stackoverflow.com/questions/4172722). Change the type of `m_group` to `Group`, or `std::unique_ptr` if you really want dynamic allocation for some reason. – Mike Seymour Aug 08 '14 at 12:32
  • `m_group` is `vector` right? I think the problem is in Mesh which is probably not being copied correctly. `Mesh` is being copied when the vector erase is called, and if `Mesh::~Mesh` or `Mesh::Mesh(const Mesh&)` or `Mesh::operator=(const Mesh&)` are undefined or wrongly defined. Alternatively someone somewhere is retaining a reference to a `Mesh` which no longer exists. – Ben Aug 08 '14 at 14:17
  • I won't have access to the code until monday. Can't try any given solution or post additional code right now, sry – jeromintus Aug 08 '14 at 20:37
  • I've added more code now! – jeromintus Aug 11 '14 at 06:38
  • I'm kinda sure it has to do with the erase – jeromintus Aug 11 '14 at 06:43
  • `xIndex.erase(yIndex.begin() + index);` you are deleting from xIndex using an iterator to element from yIndex. – Dragos Calin Aug 11 '14 at 08:51
  • omg. i think i went about 100 times through the code and didn't notice this simple mistake.... I'll change to a 2D vector now anyway. Thanks @Dragos Calin and everyone who went through the code – jeromintus Aug 11 '14 at 08:57

1 Answers1

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

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

jeromintus
  • 367
  • 1
  • 5
  • 14