title: C++ Unhandled exception: An invalid parameter was passed to a function that considers invalid parameters fatal
body: <p>I'm doing a group project at school, we're creating a 2d game, and I'm getting an exception I have no idea what to do with.</p>
  
  <p>A side note: all of this is done in debug mode.</p>
  
  <p>We have a physicsengine class that calculates collision and adds vector forces to entities, the code:</p>
  
  <pre><code>void PhysicsEngine::doPhysicsTick()
  {
      vector&lt;vector&lt;unordered_set&lt;Entity*&gt;&gt;&gt; *grid = this-&gt;grid.getGrid();
      unordered_set&lt;Entity*&gt;* updatable = new unordered_set&lt;Entity*&gt;();
      unordered_set&lt;Entity*&gt;* collidable = new unordered_set&lt;Entity*&gt;();
  
      const int &amp;r = CollisionGrid::C_GRID_RADIUS;
  
      for (int row = 0; row &lt; grid-&gt;size(); row++)
      for (int col = 0; col &lt; grid-&gt;at(row).size(); col++)
      {
          collidable-&gt;clear();
  
          // put all surrounding grid blocks in a entity list to
          // check against collision with the current block
  
          int rw(row - r &gt; 0 ? row - r : 0);
          int cl(col - r &gt; 0 ? col - r : 0);
  
          for (int rrow = rw; rrow &lt;= row + r &amp;&amp; rrow &lt; grid-&gt;size(); rrow++)
          for (int rcol = cl; rcol &lt;= col + r &amp;&amp; rcol &lt; grid-&gt;at(rrow).size(); rcol++)
              for (Entity *e : grid-&gt;at(rrow).at(rcol)) // It crashes here
                  collidable-&gt;insert(e);
  
          for (Entity *e : grid-&gt;at(row).at(col))
          {
              if (e-&gt;isFixed())
                  continue;
  
              e-&gt;speed += gravity;
  
              eBounds.reBox(e-&gt;location, e-&gt;getSize() + e-&gt;speed);
  
              for (Entity *c : *collidable)
                  if (isOverlapping(eBounds, Box(c, c-&gt;getSize())) &amp;&amp; e != c)
                      doCollision(e, c);
  
              updatable-&gt;insert(e);
          }
      }
  </code></pre>
  
  <p>We have a separate collisiongrid class that manages the grid where we store our entities for collision check (so we don't have to check everything with everything)</p>
  
  <p>the collisiongrid and its underlying <code>vector</code> are created when the constructor of the physicsengine is called <code>PhysicsEngine::PhysicsEngine(World *world) : grid(world)</code>. But for some reason on only the first tick I see the vector grid points to something nonexistant (size being ridiculously large and such) inside of the loop that fills up collidable.</p>
  
  <p>The error it throws is in the title. If I put a catch block around it, it'll just crash someplace else in some c++ library kind of randomly (different file every time)</p>
  
  <p>and for some reason it crashes in our gameloop class' (the one calling tick of physicsengine) destructor if I don't comment the thing I put in there.</p>
  
  <pre><code>Gameloop::~Gameloop()
  {
      //if( t ) // t = #include &lt;thread&gt;
      //  delete t;
  }
  </code></pre>
  
  <p>We are not allowed to use any libraries outside of the Win32 API and some default c++ libraries</p>
  
  <hr>
  
  <p>Edit: I added some pictures (and undid the dynamic allocation of the unordered_sets)</p>
  
  <p>what it should show:</p>
  
  <p><img src="https://i.stack.imgur.com/l9SQR.png" alt="correct grid"></p>
  
  <p>what it sometimes shows on the first tick:
  (notice how the pointers are the same, first two shot where made during the same run)</p>
  
  <p><img src="https://i.stack.imgur.com/lKL0r.png" alt="enter image description here"></p>
  
  <p>what it shows other times on the first tick:</p>
  
  <p><img src="https://i.stack.imgur.com/ePWV9.png" alt="enter image description here"></p>