使用vector::erase会导致malloc():内存损坏(快速)c++
using vector::erase causes a malloc(): memory corruption (fast) c++
我有一个Group类,它有一个向量v_Group;和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();
}
在我的另一个类中,我正在创建一个Group对象:
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;
}
在其他地方,我想使用从初始组向量中删除一个对象
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.erase(yIndex.begin() + index);
之后,程序因分段故障而崩溃或者当我试图再次调用Group中的向量时。我得到一个malloc((:内存损坏(快速(错误。
也许在擦除一个元素后,内存仍然被分配给它,因此我会出现这样的错误?
有人能帮忙吗?
yIndex.erase(yIndex.begin() + index);
xIndex.erase(yIndex.begin() + index);
在xIndex.erase((中,我使用了yIndex,而不是xIndex。可怕的错误。
相关文章:
- 快速 TS 片段 ffmpeg 解码 - 内存泄漏
- 实现快速排序的内存问题
- 用于小未对齐数据的快速内存
- 直观的内存布局,可实现快速SIMD/面向数据的设计
- 释放内存失败:free():下一个大小无效(快速):
- 有什么快速访问内存的技巧吗
- malloc():内存损坏(快速),std::vector 和 std::stringstream
- 错误消息glibc检测到Malloc():内存损坏(快速)
- C++malloc()内存损坏(快速)
- 如何将 6 字节无符号整数快速复制到内存区域
- malloc错误“命名管道打开时内存损坏(快速)
- 比较快速两个内存区域
- 内存错误: free(): 下一个大小无效 (快速)
- 是否有一个快速的内存中队列,我可以使用它在达到一定大小时交换项目
- 快速排序和合并排序坏内存分配(不对 32k 元素进行排序)
- 将一系列整数写入全局内存的快速(est)方法
- malloc():内存损坏(快速)错误C++
- C++内存的快速分配
- 使用c++和boost内存映射文件读取txt文件并快速解析它们
- 我的阵列是导致 CPU 和内存快速累积的原因吗?