从矢量消除时的C++分割故障
不是重复的,因为我已经尝试了该问题中使用的解决方案 所以我在做一个游戏引擎,我有一个叫做Scene的类,它有一个游戏对象的向量 场景有一个名为destantate的函数,该函数用于删除游戏对象 调用此函数时,在尝试使用std::vector::erase函数时会出现分段错误 我认为这可能与另一个循环迭代向量有关,它试图访问向量中已擦除的指针 请看下面的代码:从矢量消除时的C++分割故障,c++,c++11,pointers,segmentation-fault,stdvector,C++,C++11,Pointers,Segmentation Fault,Stdvector,不是重复的,因为我已经尝试了该问题中使用的解决方案 所以我在做一个游戏引擎,我有一个叫做Scene的类,它有一个游戏对象的向量 场景有一个名为destantate的函数,该函数用于删除游戏对象 调用此函数时,在尝试使用std::vector::erase函数时会出现分段错误 我认为这可能与另一个循环迭代向量有关,它试图访问向量中已擦除的指针 请看下面的代码: #include "Scene.h" #include "Instance.h" Scene::Scene() { this-
#include "Scene.h"
#include "Instance.h"
Scene::Scene() {
this->camera = new Camera(0, 0);
}
void Scene::instantiate(Instance *instance) {
this->instances->push_back(instance);
}
void Scene::destantiate(Instance &instance) {
instance.trash = true;
}
void Scene::tick(float delta) {
this->camera->tick(delta);
for(std::vector<Instance*>::iterator it = this->instances->begin(); it != this->instances->end(); ++it) {
if ((*it)->trash) {
std::vector<Instance*>::iterator position = std::find(
this->instances->begin(),
this->instances->end(),
(*it)
);
if (position != this->instances->end()) {
this->instances->erase(position);
}
continue;
}
(*it)->collisionBox->width = (*it)->sprite->getCurrentImage()->getWidth();
(*it)->collisionBox->height = (*it)->sprite->getCurrentImage()->getHeight();
(*it)->tick(delta);
}
}
void Scene::draw(float delta) {
this->camera->draw(delta);
for(std::vector<Instance*>::iterator it = this->instances->begin(); it != this->instances->end(); ++it) {
if ((*it)->trash) { continue; }
glPushMatrix();
if ((*it)->centeredOrigo) {
glTranslatef((*it)->x - (*it)->sprite->getCurrentImage()->getWidth()/2, (*it)->y - (*it)->sprite->getCurrentImage()->getHeight()/2, 0.0f);
} else {
glTranslatef((*it)->x, (*it)->y, 0.0f);
}
if ((*it)->centeredOrigo) {
glTranslatef(((*it)->sprite->getCurrentImage()->getWidth()/2), ((*it)->sprite->getCurrentImage()->getHeight()/2), 0);
}
glRotatef((*it)->rotation, 0.0f, 0.0f, 1.0f);
if ((*it)->centeredOrigo) {
glTranslatef(-((*it)->sprite->getCurrentImage()->getWidth()/2), -((*it)->sprite->getCurrentImage()->getHeight()/2), 0);
}
(*it)->draw(delta);
glPopMatrix();
}
}
输出:
Segmentation fault: 11
valgrind谈到了未初始化指针的使用,我想你误解了我的意思。我将尝试用代码进行解释:
void deinstantiate(GameObject go) {
flaggedForDeletion.push_back(go);
}
void tick(float delta) {
for(auto it = gameObjects.begin(); it != gameObjects.end(); ++it) {
it->tick(delta);
}
for(auto it = flaggedForDeletion.begin(); it != flaggedForDeletion.end(); ++it) {
std::remove(vec.begin(), vec.end(), *it);
}
}
因此,您只需存储要删除的对象,然后再删除它们。在对它们进行迭代时不能删除它们,因为这会使迭代器无效
除非解决方案对您有效。是的,您不能在迭代向量时对其进行变异。但是你不应该有这样一个垃圾场。您可能需要第二个向量来存储应该删除的对象。另一个选择是对向量的副本进行操作,但我不建议这样做。您尝试过吗?@User9182736455 hmm存储应该删除的对象听起来很有趣。但假设我在它上面迭代delete向量,并从主向量中删除内容。在主向量上迭代的迭代器会不会中断并导致分段错误?因此,基本上,您正在创建另一个指向同一元素的迭代器,使用std::find,通过另一个迭代器进行擦除,希望初始迭代器保持有效?不,它不是那样工作的。@A.S.H那么我怎样才能以一种有效的方式正确地做这件事呢?这解决了我的问题!谢谢D
void deinstantiate(GameObject go) {
flaggedForDeletion.push_back(go);
}
void tick(float delta) {
for(auto it = gameObjects.begin(); it != gameObjects.end(); ++it) {
it->tick(delta);
}
for(auto it = flaggedForDeletion.begin(); it != flaggedForDeletion.end(); ++it) {
std::remove(vec.begin(), vec.end(), *it);
}
}