title: Debug Assertion Failed! Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
body: <p>I know this problem is assessed many times on these forums, but they really are unique to their specific cases most times.</p>
  
  <p>This is a project for a class (on C++ no less), and the point of the project was to remake the classic board game Reversi.</p>
  
  <p>I have toiled through code for hours and finally made a program that will work, or so I thought!</p>
  
  <p>The big problem I am having seems to come from my deconstructor as it's giving me this error many of us have seen. My code is posted below and from my own debugging code (using helpful cout messages) I have determined that the program manages to run to the end of the the Game.cpp class. Only, it stumbles on the deconstructor and crashes before finally "ending nicely".</p>
  
  <p>Board.h</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;vector&gt;
  #ifndef BOARD_H
  #define BOARD_H
  using namespace std;
  
  enum Piece {LIGHT, DARK, EMPTY, BORDER};
  typedef int Move;
  Move const NullMove = -1;
  int const MAX_SQUARES = 100;
  enum Direction {N=0, NE=1, E=2, SE=3, S=4, SW=5, W=6, NW=7};
  
  
  class Board
  {
  public:
      Board();
      void reset();
      void display();
      void makeMove(Piece, Move);
      bool isLegal(Piece, Move);
      Piece getWinner();
      Piece getPlayer();
      void genMoves();
      int numMoves();
      Move getMove(int) const;
      bool gameOver;
  
  private:
      Piece board[MAX_SQUARES];
      int lightPieces;
      int darkPieces;
      vector&lt;Move&gt; goodMoves;
      static Piece currentPlayer;
      vector &lt;int&gt; offset;
  };
  
  #endif
  </code></pre>
  
  <p>Board.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;vector&gt;
  #include "Board.h"
  using namespace std;
  
  
  Board::Board()
  {
      reset();
      for(int i=0;i&lt;MAX_SQUARES;++i)
      {
          if(i&lt;11 || i&gt;88 || i%10==0 || i%10==9)
              board[i]=BORDER;
      }
  
      offset.push_back(10);
      offset.push_back(11);
      offset.push_back(1);
      offset.push_back(-9);
      offset.push_back(-10);
      offset.push_back(-11);
      offset.push_back(-1);
      offset.push_back(9);
  
      board[44] = LIGHT;
      board[45] = DARK;
      board[54] = DARK;
      board[55] = LIGHT;
  
      gameOver=false;
  }
  
  void Board::reset()
  {
      for(int i=0; i&lt;MAX_SQUARES;++i)
          board[i] = EMPTY;
  }
  
  void Board::display()
  {
      for(int i=0;i&lt;MAX_SQUARES;++i)
      {
          switch(board[i])
          {
              case LIGHT:
                  cout &lt;&lt; "|LG|";
                  break;
              case DARK:
                  cout &lt;&lt; "|DR|";
                  break;
              case EMPTY:
                  cout &lt;&lt; "|  |";
                  break;
              case BORDER:
                  if(i&lt;9)
                      cout &lt;&lt; "&lt;0" &lt;&lt; i &lt;&lt; "&gt;";
                  else if(i==9)
                      cout &lt;&lt; "&lt;09&gt;\n----------------------------------------\n";
                  else if(i%10==9)
                      cout &lt;&lt; "&lt;$$&gt;\n----------------------------------------\n";
                  else if(i%10==0)
                      cout &lt;&lt; "&lt;" &lt;&lt; i &lt;&lt; "&gt;";
                  else if(i&lt;11 || i&gt;90)
                      cout &lt;&lt; "&lt;$$&gt;";
                  break;
          }
  
      }
  }
  
  void Board::makeMove(Piece p, Move m)
  {
      genMoves(); //generate valid moves
      cout &lt;&lt; "generated moves\n";
      int good = numMoves();  //gets number of moves
      if(good&gt;0)  //if there are valid moves
      {
          cout &lt;&lt; "more than 0\n";
          for(int i=0;i&lt;goodMoves.size();++i) //checking the valid move list
          {
              if(m==goodMoves[i]) //if our move is in the list
              {
                  cout &lt;&lt; "move was in list\n";
                  board[m]=p; //change square
                  if(board[m]==DARK)
                      cout &lt;&lt; "ITS DARK\n";
                  else if(board[m]==LIGHT)
                      cout &lt;&lt; "ITS LIGHT\n";
                  else if(board[m]==EMPTY)
                      cout &lt;&lt; "ITS EMPTY WTF WTF WTF\n";
                  for(int i=0;i&lt;8;++i)    //checking directions
                  {
                      Piece opp =(p==LIGHT)? DARK : LIGHT;    //Making an opposite piece
                      cout &lt;&lt; "made opp\n";
                      int counter=0;
                      int toCheck = m+offset[i];  //making the next square to check
                      if(board[toCheck]==opp) //if it's the opposite colour from player
                      {
                          cout &lt;&lt; "it was the opposite piece\n";
                          while(board[toCheck]!=BORDER &amp;&amp; board[toCheck]!=EMPTY)  //while it's a piece
                          {
                              cout &lt;&lt; "there was a piece to check\n";
                              if(board[toCheck]==p &amp;&amp; counter&gt;0)  //if it's player's piece and counter is higher than 0
                              {
                                  cout &lt;&lt; "this should flip stuff\n";
                                  for(int k=m;k!=toCheck;k = k+offset[i])
                                  {
                                      board[k]=p;
                                      cout &lt;&lt; k;
                                  }
                                  break;
                              }
                              else
                              {
                                  cout &lt;&lt; "found nothing, keep trying..\n";
                                  toCheck += offset[i];   //if not, step in direction
                                  counter++;
                              }
                          }
                      }
                  }
              }
              cout &lt;&lt; "move wasn't in list\n";
          }
      }
      currentPlayer=(p==LIGHT)? DARK : LIGHT;
  }
  
  bool Board::isLegal(Piece p, Move m)
  {
      Piece opp =(p==LIGHT)? DARK : LIGHT;    //Making an opposite piece
      if(board[m]==EMPTY) //Checking that the space we're going is empty
      {
          for(int i=0;i&lt;8;++i)    //checking directions
          {
              int toCheck = m+offset[i];  //making the next square to check
              if(board[toCheck]==opp) //if it's the opposite colour from player
              {
                  while(board[toCheck]!=BORDER &amp;&amp; board[toCheck]!=EMPTY)  //while it's a piece
                  {
                      if(board[toCheck]==p)   //if it's player's piece
                          return true;    // if move is valid
                      else
                          toCheck += offset[i];   //if not, step in direction
                  }
              }
          }
          return false;   // if there's no valid direction moves
      }
      else    // if it's not empty
          return false;
  }
  
  Piece Board::getWinner()
  {
      bool gameDone = true;
  
      for(int i=0;i&lt;MAX_SQUARES;++i)
      {
          if(board[i]==EMPTY)
              gameDone = false;
      }
  
      if(gameDone==false)
          return EMPTY;
      else if(lightPieces&gt;darkPieces)
          return LIGHT;
      else
          return DARK;
  }
  
  Piece Board::getPlayer()
  {
      return currentPlayer;
  }
  
  void Board::genMoves()
  {
      goodMoves.clear();
      cout &lt;&lt; "generating shit\n";
      for(int i=0;i&lt;MAX_SQUARES;++i)
      {
          if(isLegal(currentPlayer, i))
              {goodMoves.push_back(i);
          cout &lt;&lt; i &lt;&lt; " twas a good move\n";}
      }
      if(goodMoves.size()==0)
          gameOver=true;
  }
  
  int Board::numMoves()
  {
      return goodMoves.size();
  }
  
  Move Board::getMove(int i) const
  {
      return goodMoves[i];
  }
  
  Piece Board::currentPlayer=DARK;
  </code></pre>
  
  <p>Player.h</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #ifndef PLAYER_H
  #define PLAYER_H
  #include "Board.h"
  using namespace std;
  
  
  
  class Player
  {
  public:
      Player(const string&amp;, Piece);
      Piece getPiece() const;
      virtual void makeMove(Board&amp;)=0;
      void setName(string&amp;);
      string getName();
  private:
      string name;
      Piece color;
  };
  
  #endif
  </code></pre>
  
  <p>Player.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include "Player.h"
  using namespace std;
  
  Player::Player(const string&amp; n, Piece c)
  {
      name = n;
      color = c;
  }
  
  Piece Player::getPiece() const
  {
      return color;
  }
  
  void Player::setName(string&amp; n)
  {
      name = n;
  }
  
  string Player::getName()
  {
      return name;
  }
  </code></pre>
  
  <p>HumanPlayer.h</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include "Player.h"
  #ifndef HUMANPLAYER_H
  #define HUMANPLAYER_H
  using namespace std;
  
  class HumanPlayer: public Player
  {
  public:
      HumanPlayer(const string&amp;, Piece);
      void makeMove(Board&amp;);
  };
  
  #endif
  </code></pre>
  
  <p>HumanPlayer.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include "Player.h"
  #include "HumanPlayer.h"
  using namespace std;
  
  HumanPlayer::HumanPlayer(const string&amp; n, Piece c): Player(n,c) 
  {
  
  }
  
  void HumanPlayer::makeMove(Board&amp; b)
  {
      Move goTo;
      cout &lt;&lt; "Please enter the number for the square you would like to move: ";
      cin &gt;&gt; goTo;
      if(!b.gameOver)
          b.makeMove(getPiece(),goTo);
  }
  </code></pre>
  
  <p>ComputerPlayer.h</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include "Player.h"
  #ifndef COMPUTERPLAYER_H
  #define COMPUTERPLAYER_H
  using namespace std;
  
  class ComputerPlayer: public Player
  {
  public:
      ComputerPlayer(Piece p);
  private:
      static int counter;
      //string name;
  };
  
  #endif
  </code></pre>
  
  <p>ComputerPlayer.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include "ComputerPlayer.h"
  using namespace std;
  
  ComputerPlayer::ComputerPlayer(Piece p) : Player("", p)
  {
      string name = "ComputerPlayer" + char(65+counter);
      setName(name);
  }
  
  int ComputerPlayer::counter=0;
  </code></pre>
  
  <p>RandomPlayer.h</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include "ComputerPlayer.h"
  #ifndef RANDOMPLAYER_H
  #define RANDOMPLAYER_H
  using namespace std;
  
  class RandomPlayer : public ComputerPlayer
  {
  public:
      RandomPlayer(Piece);
      void makeMove(Board&amp;);
  };
  
  #endif
  </code></pre>
  
  <p>RandomPlayer.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include "RandomPlayer.h"
  using namespace std;
  
  RandomPlayer::RandomPlayer(Piece p) : ComputerPlayer(p)
  {
  
  }
  
  void RandomPlayer::makeMove(Board&amp; b)
  {
      cout &lt;&lt; "Player 2 making move in stuff\n";
      b.genMoves();
      int temp1 = b.numMoves();
      cout &lt;&lt; "This is temp1: " &lt;&lt;temp1 &lt;&lt; '\n';
      int temp2;
      if(temp1&gt;0)
      {
          temp2 = rand()%temp1;
      }
      //cout &lt;&lt; "This is temp2: " &lt;&lt;temp2 &lt;&lt; '\n';
      if(!b.gameOver)
          b.makeMove(getPiece(),b.getMove(temp2));
  }
  </code></pre>
  
  <p>Game.h</p>
  
  <pre><code>// Name: James St-Germain
  // Student #: 0270250
  
  #include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include "Board.h";
  #include "HumanPlayer.h"
  #include "RandomPlayer.h"
  #ifndef GAME_H
  #define GAME_H
  using namespace std;
  
  class Game
  {
  public:
      Game();
      ~Game();
      void selectPlayers();
      Player* nextPlayer();
      void play();
      void announceWinner();
  private:
      Board b;
      Player *p1;
      Player *p2;
      bool isRunning;
  };
  
  #endif
  </code></pre>
  
  <p>Game.cpp</p>
  
  <pre><code>// Name: James St-Germain
  // Student #: 0270250
  
  #include "Game.h"
  #include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;string&gt;
  using namespace std;
  
  Game::Game(): b(), p1(NULL), p2(NULL), isRunning(true){}
  
  Game::~Game()
  {
      delete &amp;b;
      delete &amp;p1;
      delete &amp;p2;
  }
  
  void Game::selectPlayers()
  {
      string choice[2];
      cout &lt;&lt; "Is player 1 a human player or computer player? (H/C): \n";
      cin &gt;&gt; choice[0];
      cout &lt;&lt; "Is player 2 a human player or computer player? (H/C): \n";
      cin &gt;&gt; choice[1];
      for(int i=0;i&lt;2;++i)
      {
          if(choice[i]=="H")
          {
              string n;
              char* c;
              cout &lt;&lt; "What is your name?: \n";
              cin &gt;&gt; n;
              if(i==0)
                  p1 = new HumanPlayer(n, LIGHT);
              else
                  p2 = new HumanPlayer(n, DARK);
          }
          if(choice[i]=="C")
          {
              if(i==0)
                  p1 = new RandomPlayer(LIGHT);
              else
                  p2 = new RandomPlayer(DARK);
          }
      }
      cout &lt;&lt; "Player 1 is " &lt;&lt; p1-&gt;getName() &lt;&lt; '\n';
      cout &lt;&lt; "Player 2 is " &lt;&lt; p2-&gt;getName() &lt;&lt; '\n';
  
  
  }
  
  Player* Game::nextPlayer()
  {
      if(b.getPlayer()==LIGHT)
          return p2;
      else
          return p1;
  }
  
  void Game::play()
  {
      while(isRunning)
      {
          b.display();
          Piece temp = b.getPlayer();
          if(temp==LIGHT)
          {
              cout &lt;&lt; "Player 1 moves!\n";
              p1-&gt;makeMove(b);
          }
          else
          {
              cout &lt;&lt; "Player 2 moves!\n";
              p2-&gt;makeMove(b);
          }
  
          if(b.gameOver==true)
              break;
      }
  }
  
  void Game::announceWinner()
  {
      Piece winner = b.getWinner();
      string name = (winner==LIGHT) ? p1-&gt;getName() : p2-&gt;getName();
      cout &lt;&lt; "The winner is " &lt;&lt; name &lt;&lt; "! Congratulations!\n";
  }
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include "Game.h"
  using namespace std;
  
  int main()
  {
      Game Reversi = Game();
      Reversi.selectPlayers();
      Reversi.play();
      Reversi.announceWinner();   
  }
  </code></pre>
  
  <p>I apologize for the extreme amount of code, but at this point, I don't know what to fix. I know there might also be bad coding habits here, so if you see any, I would love the constructive criticism.</p>
  
  <p>Thank you in advance for all your help!! </p>