C++ 从矢量擦除时出现分割错误

C++ Segmentation fault when erasing from vector

本文关键字:分割 错误 擦除 C++      更新时间:2023-10-16

不是迭代向量的副本,请随时删除某些项目,因为我已经尝试了该问题中使用的解决方案!

所以我正在制作一个游戏引擎,其中我有一个名为"Scene"的类,它具有游戏对象的向量。

该场景具有一个名为"destantiate"的功能,应该用于"删除"游戏对象。

当我调用此函数时,我在尝试使用 std::vector::erase 函数时出现分割错误。

我认为这可能与另一个迭代向量的循环有关,它正在尝试访问已擦除的向量中的指针?

看看下面的代码:

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

std::vector::erase 函数在"tick"函数中被调用,它检查对象是否具有"垃圾"标志。

这是在运行时在游戏对象类中调用"destantiate"函数的地方:

#include "SDLOpenGL.h"
#include "TestObj.h"

TestObj::TestObj(float x, float y) : Instance(x, y) {
    this->sprite->addImage(game.loader->load("assets/card.png"));
    this->centeredOrigo = true;
}
void TestObj::tick(float delta) {
    if (this->trash) { return; }
    //this->x = game.getMousePosition().x;
    //this->y = game.getMousePosition().y;
    this->rotation += 2.0f;
    if (game.keyboardDown(SDL_SCANCODE_LEFT)) {
        this->x -= 9.5f;
    }
    if (game.keyboardDown(SDL_SCANCODE_RIGHT)) {
        this->x += 9.5f;
    }
    if (game.keyboardDown(SDL_SCANCODE_UP)) {
        this->y -= 9.5f;
    }
    if (game.keyboardDown(SDL_SCANCODE_DOWN)) {
        this->y += 9.5f;
        //Segmentation fault
        game.getCurrentScene()->destantiate(*this);
    }
}
void TestObj::draw(float delta) {
    if (this->trash) { return; }
    this->sprite->draw(delta);
    this->collisionBox->draw(delta);
}

输出:

Segmentation fault: 11

瓦尔格林德说了一些关于"使用未初始化的指针"的事情

我想你误解了我的意思。我将尝试用代码解释:

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

因此,您只需存储要删除的对象,然后删除它们。在迭代它们时无法删除它们,因为这会使您的迭代器无效。

除了此解决方案适合您。