title: C++ std::vector iterators error
body: <p>First of all <strong>I'm sorry for my bad english</strong>, hope you guys will understand me :) Im writing WinAPI game and my classes behave very strange: all operations with vector
  crash my program so Windows says that my .exe stopped working. But when I debug these lines
  I get exceptions.</p>
  
  <p>This is how my class header looks like:</p>
  
  <pre><code>#ifndef FIGURE_H_INCLUDED
  #define FIGURE_H_INCLUDED
  
  #include &lt;vector&gt;
  #include &lt;Windows.h&gt;
  #include "Other.h"
  
  using namespace std;
  
  enum Figure_Type { I, J, L, O, S, T, Z };
  
  class Figure
  {
  public:
      /* CONSTRUCTORS */
      Figure();
      Figure(Figure_Type);
  
      /* MOVEMENT */
      bool                Move(vector&lt;Cell&gt;&amp;, Direction&amp;);
      void                Drop(vector&lt;Cell&gt;&amp;);
      bool                Rotate(vector&lt;Cell&gt;&amp;);
  
      /* OTHER */ 
      void                Draw(HDC&amp;);
  
  private:
  
      /* METHODS */   
      void                Generate();
      void                GenerateMasks();
      void                GenerateFigure();
      Figure              GetFigureCopy() const;  
  
      /* DATA */
      Shift               shift;
      char                mask[4][4];
      vector&lt;Cell&gt;        vCells;
      Figure_Type         type;
      int                 rotation;
  };
  
  #endif
  </code></pre>
  
  <p>My constructors are using Generate() method, which code is:</p>
  
  <pre><code>void Figure::GenerateFigure()
  {
      vCells.clear();
      int defPosX = 4,
              defPosY = 20;
      Cell    cell;
  
      for(int y = 0; y &lt; 4; y++)
      {
          for(int x = 0; x &lt; 4; x++)
          {
              if(mask[y][x] == '0')
              {
                  cell.x = defPosX + x + shift.dx;
                  cell.y = defPosY - y + shift.dy;
                  vCells.push_back(cell);
              }
          }
      }
  }
  </code></pre>
  
  <p>And I'm getting exceptions on vCells.clear() method and (if I comment first line) vCells.push_back(cell) line. Actually every operation with vector / vector iterators crash my program even incrementing iterator, those are just the first so my code isn't running any longer after them.
  Exception text:</p>
  
  <p><strong>"Unhandled exception at 0x5A4ACCD2 (msvcp110d.dll) in Tetris_completely_new.exe: 0xC000041D: An unhandled exception was encountered during a user callback."</strong></p>
  
  <p>And these exceptions are thrown on 217's line of "xutility". I commented it:</p>
  
  <pre><code>....
  // MEMBER FUNCTIONS FOR _Container_base12
  inline void _Container_base12::_Orphan_all()
      {   // orphan all iterators
   #if _ITERATOR_DEBUG_LEVEL == 2
      if (_Myproxy != 0)
          {   // proxy allocated, drain it
          _Lockit _Lock(_LOCK_DEBUG);
  
          for (_Iterator_base12 **_Pnext = &amp;_Myproxy-&gt;_Myfirstiter;
              *_Pnext != 0; *_Pnext = (*_Pnext)-&gt;_Mynextiter)
              **(*_Pnext)-&gt;_Myproxy = 0;**    // &lt;------------ THIS LINE
          _Myproxy-&gt;_Myfirstiter = 0;
          }
   #endif /* _ITERATOR_DEBUG_LEVEL == 2 */
      }
  ....
  </code></pre>
  
  <p>Here is how my <strong>Cell struct</strong> looks like:</p>
  
  <pre><code>struct Cell
  {
      Cell() : x(1), y(1) { }
      Cell(int _x, int _y): x(_x), y(_y) { }
  
      void Draw(HDC&amp;) const;
  
      bool operator ==(const Cell&amp; a) const { return (x == a.x &amp;&amp; y == a.y); } 
      bool operator !=(const Cell&amp; a) const { return !(*this == a); } 
  
      int x;
      int y;
  };
  </code></pre>
  
  <p>And <strong>Figure constructor</strong>:</p>
  
  <pre><code>Figure::Figure()
  {
      srand(time(NULL));
  
      vCells.clear();
      type = Figure_Type(rand() % 7);
      rotation = 0;
      shift.dx = 0;
      shift.dy = 0;
  
      Generate();
  }
  </code></pre>