title: C++ Segmentation fault when erasing from vector
body: <p>NOT A DUPLICATE OF <a href="https://stackoverflow.com/questions/1604588">iterate vector, remove certain items as I go</a> , since I have tried the solution used in that issue!</p>
  
  <p>So I am making a game engine where I have a class called "Scene" which has a vector of game objects.</p>
  
  <p>The scene has a function called "destantiate" which should be used to "delete" game objects.</p>
  
  <p>When I call this function, I get Segmentation fault when trying to use the std::vector::erase function.</p>
  
  <p>I think it might have to do with another loop iterating the vector and it is trying to access a pointer in the vector which has been erased?</p>
  
  <p>Take a look at the code below:</p>
  
  <pre><code>#include "Scene.h"
  #include "Instance.h"
  
  
  Scene::Scene() {
      this-&gt;camera = new Camera(0, 0);
  }
  
  void Scene::instantiate(Instance *instance) {
      this-&gt;instances-&gt;push_back(instance);
  }
  
  void Scene::destantiate(Instance &amp;instance) {
      instance.trash = true;
  }
  
  void Scene::tick(float delta) {
      this-&gt;camera-&gt;tick(delta);
      for(std::vector&lt;Instance*&gt;::iterator it = this-&gt;instances-&gt;begin(); it != this-&gt;instances-&gt;end(); ++it) {
          if ((*it)-&gt;trash) {
              std::vector&lt;Instance*&gt;::iterator position = std::find(
              this-&gt;instances-&gt;begin(),
              this-&gt;instances-&gt;end(),
              (*it)
              );
  
              if (position != this-&gt;instances-&gt;end()) {
                  this-&gt;instances-&gt;erase(position);
              }
  
              continue;
          }
          (*it)-&gt;collisionBox-&gt;width = (*it)-&gt;sprite-&gt;getCurrentImage()-&gt;getWidth();
          (*it)-&gt;collisionBox-&gt;height = (*it)-&gt;sprite-&gt;getCurrentImage()-&gt;getHeight();
          (*it)-&gt;tick(delta);
      }
  }
  
  void Scene::draw(float delta) {
      this-&gt;camera-&gt;draw(delta);
      for(std::vector&lt;Instance*&gt;::iterator it = this-&gt;instances-&gt;begin(); it != this-&gt;instances-&gt;end(); ++it) {
          if ((*it)-&gt;trash) { continue; }
          glPushMatrix();
  
          if ((*it)-&gt;centeredOrigo) {
              glTranslatef((*it)-&gt;x - (*it)-&gt;sprite-&gt;getCurrentImage()-&gt;getWidth()/2, (*it)-&gt;y - (*it)-&gt;sprite-&gt;getCurrentImage()-&gt;getHeight()/2, 0.0f);
          } else {
              glTranslatef((*it)-&gt;x, (*it)-&gt;y, 0.0f); 
          }
  
          if ((*it)-&gt;centeredOrigo) {
              glTranslatef(((*it)-&gt;sprite-&gt;getCurrentImage()-&gt;getWidth()/2), ((*it)-&gt;sprite-&gt;getCurrentImage()-&gt;getHeight()/2), 0);
          }
  
          glRotatef((*it)-&gt;rotation, 0.0f, 0.0f, 1.0f);
  
          if ((*it)-&gt;centeredOrigo) {
              glTranslatef(-((*it)-&gt;sprite-&gt;getCurrentImage()-&gt;getWidth()/2), -((*it)-&gt;sprite-&gt;getCurrentImage()-&gt;getHeight()/2), 0);
          }
  
          (*it)-&gt;draw(delta);
  
          glPopMatrix();
      }
  }
  </code></pre>
  
  <p>The std::vector::erase function is being called in the "tick" function where it checks if an object has the "trash" flag.</p>
  
  <p>This is where the "destantiate" function is being called in runtime, inside of a game object class:</p>
  
  <pre><code>#include "SDLOpenGL.h"
  #include "TestObj.h"
  
  
  TestObj::TestObj(float x, float y) : Instance(x, y) {
      this-&gt;sprite-&gt;addImage(game.loader-&gt;load("assets/card.png"));
      this-&gt;centeredOrigo = true;
  }
  
  void TestObj::tick(float delta) {
      if (this-&gt;trash) { return; }
      //this-&gt;x = game.getMousePosition().x;
      //this-&gt;y = game.getMousePosition().y;
      this-&gt;rotation += 2.0f;
  
      if (game.keyboardDown(SDL_SCANCODE_LEFT)) {
          this-&gt;x -= 9.5f;
      }
      if (game.keyboardDown(SDL_SCANCODE_RIGHT)) {
          this-&gt;x += 9.5f;
      }
      if (game.keyboardDown(SDL_SCANCODE_UP)) {
          this-&gt;y -= 9.5f;
      }
      if (game.keyboardDown(SDL_SCANCODE_DOWN)) {
          this-&gt;y += 9.5f;
  
          //Segmentation fault
          game.getCurrentScene()-&gt;destantiate(*this);
      }
  }
  
  void TestObj::draw(float delta) {
      if (this-&gt;trash) { return; }
  
      this-&gt;sprite-&gt;draw(delta);
      this-&gt;collisionBox-&gt;draw(delta);
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Segmentation fault: 11
  </code></pre>
  
  <p>And valgrind says something about "use of uninitialized pointer" </p>