title: Visual studio - enabling optimization lead to crash
body: <p>I'm using visual studio 2012 on windows 8, and the 2012 november CTP.<br>
  When I run the release version of my c++ application with optimization enabled (/o2) it crashes.<br>
  It happens inside a std::vector (not the first time I use vectors though).<br>
  Stepping inside the code I found out that the second time I do a push_back, here:</p>
  
  <pre><code>1197 if (this-&gt;_Mylast == this-&gt;_Myend)
  1198     _Reserve(1);
  </code></pre>
  
  <p>if I follow _Reserve, the value of count from 1 becomes 17656451, resulting in an error later when he it calls reallocate (I have exceptions disabled).<br>
  When running in debug mode everything is fine. Also release mode with optimizations disabled is running fine.<br>
  Could it be because the compiler is a test release? I'm using some of the new features so I can't switch back and try.</p>
  
  <p>edit:</p>
  
  <p>Here is part of the code.<br>
  I have two structures:  </p>
  
  <pre><code>struct Subtile
  {
      int sequenceId;
      AnimationSequenceState sequenceState;
      XMFLOAT2 position;
  };
  
  struct Tile
  {
      //int type;
      bool visible;
      bool walkable;
  
      int x, y;
      int height;
      XMFLOAT2 position;
      float depth;
      XMVECTOR color;
  
      std::vector&lt;Subtile&gt; subtiles;
  };
  </code></pre>
  
  <p>And this function gets called from inside a loop when parsing a 2d map of a game from a lua file:</p>
  
  <pre><code>HRESULT Map::parseTile(LuaState&amp; l, int ix, int iy, XMFLOAT2 pos)
  {
      Tile tile;
  
      tile.visible = l.getBoolField(-1, parsing::MapTileVisible, true);
      tile.walkable = l.getBoolField(-1, parsing::MapTileWalkable, true);
      tile.height = l.getIntField(-1, parsing::MapTileHeight, 0);
  
      bool sync = l.getBoolField(-1, parsing::MapTileSync, true);
  
      tile.x = ix;
      tile.y = iy;
  
      tile.position.x = pos.x;
      tile.position.y = pos.y;
      tile.depth = -static_cast&lt;float&gt;(ix + iy);
      tile.color = XMVectorSet(1.0f, 1.0f, 1.0f, 1.0f);
  
      l.pushString(parsing::MapTileSubtiles);
      l.getTable();
      {
          for(l.pushNil(); l.next(); l.pop())
          {
              Subtile subtile;
              subtile.sequenceId = l.toInt();
              _sequences[subtile.sequenceId].update(subtile.sequenceState);
              if(!sync)
              {
                  subtile.sequenceState.elapsed = rand() % 1000; //一秒、適当に
              }
              tile.subtiles.push_back(subtile);
          }
      }
      l.pop();
  
      _tiles.push_back(tile); //second time crashes here
  
      return S_OK;
  }
  </code></pre>
  
  <p>edit2:</p>
  
  <p>Here is a more complete part of the code -> <a href="http://pastebin.com/ep5Xs3b9" rel="nofollow">LINK</a></p>
  
  <p>I tried to localize the problem and here is what I found.<br>
  Removing the vector at line 113 seems to have no effect but, commenting the vector at line 18 (and the code that writes/read it) solves it somehow.<br>
  There's a catch though, now I get an error from line 297, where after loading all the tiles I sort them. This problem goes away by commenting the for loop at 288-292.<br>
  Anyway I still don't understand why.</p>