title: Allocated memory release before end of program
body: <p>The program I'm writing is a Sudoku puzzle. Each puzzle is a 2D array of struct, each struct is an int and a bool. The bool represents if the number in each element of the array is a clue or not, clues cannot be changed. Sorry for huge blocks of code. I pared it down as much as possible for easy readability.</p>
  
  <p>There is a missing function ShowBoardAns from Sudoku.h. It does the same as ShowBoard, but in different colors.</p>
  
  <p>After the first time my program displays the puzzle (using ShowBoard from Sudoku.h), it appears that both Puzzle and Comp are deleted from memory. After entering a number for an answer (say 1 for x, 1 for y and 1 for answer), when the board is printed again, it either holds garbage or does not print anything except the outline. </p>
  
  <p>When debugging using Visual Studio, Puzzle and Comp start to hold garbage rather than the numbers the program assigned. As far as I can tell, this happens after the program goes back to Main from Sudoku after displaying the puzzle the first time.</p>
  
  <pre><code>#ifndef Sudoku_h
  #define Sudoku_h
  
  #include &lt;iostream&gt;
  #include &lt;Windows.h&gt;    // Header file needed to change the text color of                       console
  using namespace std;
  
  class Sudoku
  {
  protected:
  struct Puzz {
      int Ans;
      bool IsClue;
  };
  public:
  
  Sudoku() {};        // Default constructor
  Sudoku(Puzz [][9]); // Constructor
   ~Sudoku()  {}; // Destructor
  bool IsCorrect(int [][9], Puzz [][9]);      // Checks the puzzle to determine accuracy
  void ShowBoard(Puzz [][9]) const;
  void ShowBoardAns(Puzz [][9]) const;
  };
  
  
  /* Constructor
  Each puzzle array is initalized here with the complete numbers.
  */
  Sudoku::Sudoku(Puzz Arr[9][9])
  {
  ShowBoard(Arr);
  }
  
  bool Sudoku::IsCorrect(int Comp[][9], Puzz Arr[9][9])
  {
  bool Correct = false;   // Init to false
  for (int x = 0; x &lt; 9; x++)
  {
      for (int y = 0; y &lt; 9; y++)
      {
          if (Comp[x][y] != Arr[x][y].Ans)                    
              return Correct = false;
          else
              Correct = false;
      }
  }
  
  return Correct; }
  
  /*
  ShowBoard displays the incomplete puzzle to the user the first time that
  they see it. After they have started entering answers, ShowBoardAns is called         
  to show the user different text colors.
  */
  void Sudoku::ShowBoard(Puzz Arr[][9]) const
  {
  HWND console = GetConsoleWindow();
  RECT r;
  GetWindowRect(console, &amp;r);
  MoveWindow(console, r.left, r.top, 800, 450, TRUE);
  HANDLE hConsole;
  int Color = 7;
  hConsole = GetStdHandle(STD_OUTPUT_HANDLE); 
  SetConsoleTextAttribute(hConsole, Color);
  int x, y;
  cout &lt;&lt; "  ";
  for (int c = 1 ; c &lt;= 9; c++)
  {
      cout &lt;&lt; "   " &lt;&lt; c;
  }
  cout &lt;&lt; "\n   _____________________________________\n" &lt;&lt; endl;
      for (x = 0; x &lt; 9; x++)
      {
          cout &lt;&lt; x+1 &lt;&lt; "  | ";
          for (y = 0; y &lt; 9; y++)
          {
              // To assign the colors, if the element is a clue, it is green,
              // otherwise, 0s are white.
              if (Arr[x][y].IsClue == true)
              {
                  Color = 10;
                  SetConsoleTextAttribute(hConsole, Color);
                  cout &lt;&lt; Arr[x][y].Ans;
                  Color = 7;
                  SetConsoleTextAttribute(hConsole, Color);
                  cout &lt;&lt; " | ";
              }
              else 
              {
                  Color = 15;
                  SetConsoleTextAttribute(hConsole, Color);
                  cout &lt;&lt; Arr[x][y].Ans;
                  Color = 7;
                  SetConsoleTextAttribute(hConsole, Color);
                  cout &lt;&lt; " | ";
              }
          }
          cout &lt;&lt; "\n   _____________________________________\n" &lt;&lt; endl;
      }
  }
  
  
  #endif
  
  #include &lt;iostream&gt;
  #include "Sudoku.h"
  using namespace std;
  
  class Easy : protected Sudoku
  {
  private:
  int Comp[9][9];     // Comp is the completed puzzle
  Puzz Puzzle[9][9];  // Puzzle is what the user sees
  Sudoku EasyPuzzle;
  
  public:
  Easy();
  Easy(int);
  ~Easy()
  { delete [] &amp;Puzzle; }
  void AddNum(int, int, int); // Add a number to solve the puzzle
  bool Solve();
  void ShowPuzz();
  };
  
  Easy::Easy()
  {
  for (int x = 0; x &lt; 9; x++)
  {
      for (int y = 0; y &lt; 9; y++)
      {
          Comp[x][y] = 0;
          Puzzle[x][y].Ans = 0;
          Puzzle[x][y].IsClue = false;
      }
  }
  }
  
  /*
  The default constructor initializes the arrays to hold the
  completed and partial puzzles.
  */
  Easy::Easy(int A)
  {
  int Comp[][9] = { {3, 7, 6, 1, 4, 2, 9, 5, 8},
  {5, 8, 4, 6, 7, 9, 2, 1, 3},
  {9, 1, 2, 8, 3, 5, 4, 7, 6},
  {6, 9, 1, 2, 5, 4, 3, 8, 7},
  {8, 4, 5, 3, 6, 7, 1, 9, 2},
  {2, 3, 7, 9, 1, 8, 5, 6, 4},
  {4, 6, 9, 5, 8, 3, 7, 2, 1},
  {7, 2, 8, 4, 9, 1, 6, 3, 5},
  {1, 5, 3, 7, 2, 6, 8, 4, 9} };
  
  
  Puzz Puzzle[][9] = { {{0, false}, {0, false}, {0, false}, {0, false}, {4,   true},  {0, false}, {9, true},  {0, false}, {0, false}},
  {{0,  false},  {8,  true},   {0,  false},  {6,  true},   {7,  true},   {0,      false},  {0,  false},  {0,  false},  {0,  false}},
  {{9,  true},   {0,  false},  {2,  true},   {8,  true},   {0,  false},  {0,  false},  {4,  true},   {0,  false},  {0,  false}},
  {{0,  false},  {9,  true},   {1,  true},   {0,  false},  {0,  false},  {0,  false},  {0,  false},  {0,  false},  {0,  false}},
  {{0,  false},  {4,  true},   {0,  false},  {3,  true},   {6,  true},   {0,  false},  {0,  false},  {0,  false},  {2,  true }},
  {{0,  false},  {0,  false},  {0,  false},  {0,  false},  {0,  false},  {0,  false},  {5,  true},   {0,  false},  {4,  true }},
  {{0,  false},  {0,  false},  {0,  false},  {0,  false},  {0,  false},  {0,  false},  {7,  true},   {0,  false},  {1,  true }},
  {{0,  false},  {2,  true},   {8,  true},   {0,  false},  {0,  false},  {1,  true},   {0,  false},  {3,  true},   {0,  false}},
  {{1,  true},   {0,  false},  {3,  true},   {7,  true},   {0,  false},  {6,  true},   {8,  true},   {0,  false},  {0,  false}} };
  
  Sudoku EasyPuzzle(Puzzle);
  }
  
  /*
  AddNum lets a user select the X and Y coordinates of where
  they would like to enter a new number and, after checking to 
  make sure it is not one of the hint numbers, will update the 
  Puzzle array
  */
  void Easy::AddNum(int X, int Y, int Answer)
  {
  // Subtract one from both X and Y to look at the correct subscript.
  X-= 1;
  Y-= 1;
  if (Puzzle[X][Y].IsClue == true) 
  {
          cout &lt;&lt; "Cannot change a puzzle clue. Please try again." &lt;&lt; endl;
          return;
  }
  else
  {
      Puzzle[X][Y].Ans = Answer;
      EasyPuzzle.ShowBoardAns(Puzzle);
  }}
  
  
  #include &lt;iostream&gt;
  #include "Sudoku.h"
  #include "Easy.h"
  #include &lt;Windows.h&gt;
  
  using namespace std;
  
  int Menu(); // Menu function prototype
  void EasyPuzz(Easy);
  
  // Constants for the menu choices
  const int EASY = 1,
        MED = 2,
        HARD = 3,
        QUIT = 4;
  
  int main()
  {
  int Choice;
  
  
  cout &lt;&lt; "\t\t\tWelcome to Sudoku!\n\n" &lt;&lt; endl;
  
  
  Choice = Menu();    // Calls the menu function
  if (Choice == 1)
  {
      cout &lt;&lt; "You have chosen an easy puzzle. When doing the puzzle, you "
           &lt;&lt; "will enter an X and\nY coordinate to change an answer. If you "
           &lt;&lt; "enter a coordinate that is a clue, you will receive an error. " 
           &lt;&lt; "If you'd like to check to see if you've solved the puzzle "
           &lt;&lt; "type S instead of coordinates.\n" &lt;&lt; endl;
      Easy EPuzzle(1);
      //EPuzzle.ShowPuzz();
      EasyPuzz(EPuzzle);
  }
  else if (Choice == 2)
  {
      cout &lt;&lt; "You have chosen a medium puzzle. When doing the puzzle, you "
           &lt;&lt; "will enter an X and\nY coordinate to change an answer. If you "
           &lt;&lt; "enter a coordinate that is a clue, you will receive an error."
           &lt;&lt; "If you'd like to check to see if you've solved the puzzle "
           &lt;&lt; "type S instead of coordinates.\n"&lt;&lt; endl;
      Medium MPuzzle;
      MedPuzz(MPuzzle);
  }
  else if (Choice == 3)
  {
      cout &lt;&lt; "You have chosen a hard puzzle. When doing the puzzle, you "
           &lt;&lt; "will enter an X and\nY coordinate to change an answer. If you "
           &lt;&lt; "enter a coordinate that is a clue, you will receive an error. "
           &lt;&lt; "You can get a hint by entering H instead of a number."
           &lt;&lt; "If you'd like to check to see if you've solved the puzzle "
           &lt;&lt; "type S instead of coordinates.\n"&lt;&lt; endl;
      Hard HPuzzle;
      HardPuzz(HPuzzle);
  }
  else if (Choice == 4)
  {
      cout &lt;&lt; "Thank you for playing!" &lt;&lt; endl;
      exit(0);
  }
  </code></pre>