从向量中擦除时C ++分段错误

时间:2017-01-07 11:54:35

标签: c++ c++11 pointers segmentation-fault stdvector

不是iterate vector, remove certain items as I go的重复,因为我已尝试过该问题中使用的解决方案!

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

该场景有一个名为" destantiate"应该用来"删除"游戏对象。

当我调用此函数时,在尝试使用std :: vector :: erase函数时出现Segmentation fault。

我认为它可能与迭代矢量的另一个循环有关,它试图访问已被删除的向量中的指针?

看看下面的代码:

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

在&#34; tick&#34;中调用std :: vector :: erase函数。功能,它检查一个对象是否有&#34;垃圾&#34;标志。

这就是&#34;去除&#34;函数在运行时调用,在游戏对象类中:

#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

valgrind说了一些关于&#34;使用未初始化的指针&#34;

1 个答案:

答案 0 :(得分:1)

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

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

因此,您只需存储要删除的对象,然后将其删除。迭代它们时不能删除它们,因为它会使迭代器失效。

除了this解决方案适合您。