title: How do I correctly use SDL_FreeSurface when dealing with a vector of surfaces
body: <p>I have setup a small shooter game as a tutorial for myself in SDL. I have a struct of a projectile</p>
  
  <pre><code>struct projectile
  {
      SDL_Surface* surface;
      int x;
      int y;
  };
  </code></pre>
  
  <p>And I put that into a vector.</p>
  
  <pre><code>vector&lt;projectile&gt; shot;
  projectile one_shot;
  </code></pre>
  
  <p>And when I press space I create a new projectile and add it to the vector and then they're blitted when they're rendered.</p>
  
  <p>This works fine, but I'm in seemingly random cases getting a "program has stopped working" error.</p>
  
  <p>So I'm wondering what is the proper way to free the surfaces.</p>
  
  <ul>
  <li>Do I free them all afterwards?</li>
  <li>Do I free each individual shot when it exits the screen?</li>
  <li>Or some other choice?</li>
  </ul>
  
  <p>UPDATE:</p>
  
  <p>I have found where it crashes when I quit, when I have fired a few shots and they have all exited the screen. I have tried replacing the code that adds the surface to the vector with the "proper way to duplicate" as <a href="http://www.libsdl.org/cgi/docwiki.cgi/SDL_Surface" rel="nofollow noreferrer">seen in this example</a>, and it still behaves in the same way.</p>
  
  <p>This is how I free the surface.</p>
  
  <pre><code>if(shot.at(i).y &lt; 0 - shot.at(i).surface-&gt;h)
  {
      SDL_FreeSurface(shot.at(i).surface);
      shot.erase(shot.begin() + i);
  }
  </code></pre>
  
  <p>Anyone have an idea or some sample code I can look at to figure this out.</p>