title: glibc detected - double free or corruption
body: <p>this might be a bit long so my apologies.
  consider the following code (i've left some irrelevant parts from it). this code receives a pointer to a struct (BoardP theBoard), x &amp; y coords and a value.
  the goal is to place the value in a 2D array that is found in the struct.
  if the coords are out of bounds, i have to increase the size of the table, copy old data to new data and place the value in its place.
  well this code works the first call but in the second call it crashes and writes:  </p>
  
  <pre><code>*** glibc detected *** ./b: double free or corruption (top): 0x092ae138 ***  
  </code></pre>
  
  <p>i couldn't find an answer to it and i hope you will help.<br>
  These are the calls from main()</p>
  
  <pre><code>BoardP p = CreateNewBoard(10,10);
  PutBoardSquare(p,10,5,'X');
  PutBoardSquare(p,5,10,'O');
  
  Boolean PutBoardSquare(BoardP theBoard, int X, int Y, char val) {
  
      if (inBounds(X,Y,theBoard-&gt;_rows,theBoard-&gt;_cols)) {
          theBoard-&gt;_board[X * theBoard-&gt;_cols + Y] = val;
          return TRUE;
      }
      else {
          int newRows = (X&gt;=theBoard-&gt;_rows) ? (2*X) : theBoard-&gt;_rows;
          int newCols = (Y&gt;=theBoard-&gt;_cols) ? (2*Y) : theBoard-&gt;_cols;
          BoardP newBoard = CreateNewBoard(newCols,newRows);  //this creates a new Board with the new dimensions
          if (newBoard == NULL) {
              //ReportError(MEM_OUT);
              return FALSE;
          }
          else {
              copyData(theBoard,newBoard);
              freeBoardArray(&amp;theBoard-&gt;_board[0]); //free old array
              theBoard-&gt;_board = newBoard-&gt;_board;  //old array point to new array
              FreeBoard(newBoard);  //free the temp copy THIS CAUSES THE PROBLEM  
              PutBoardSquare(theBoard,X,Y,val);//recursion, will be in bounds now
              return TRUE;
          }
      }
  }
  </code></pre>
  
  <p>These are the Free functions:</p>
  
  <pre><code>void FreeBoard(BoardP board) {
      if (board != NULL) {
          printf("FREE 1\n");
          //free the board array:
          if (board-&gt;_board != NULL) {
              printf("FREE 2\n");
              freeBoardArray(&amp;board-&gt;_board[0]);
              printf("FREE 3\n");
          }
          free(board);
      }
  }
  
  static void freeBoardArray(char * arrP) {
      free(arrP);   //**PROGRAM CRASH HERE**
  }
  </code></pre>
  
  <p>This is how i create a new board:</p>
  
  <pre><code>BoardP CreateNewBoard(int width, int high) {
      BoardP board = (BoardP) malloc(sizeof(Board));
      if (board != NULL) {
          board-&gt;_board = allocateBoardArray(high,width);
          if ( board-&gt;_board == NULL) {
              FreeBoard(board);
              //TODO make file ReportError(MEM_OUT);
              return NULL;
          }
          initializeBoard(board,high,width,X_SIGN,SPACE);
          return board;
      }
      else {
          FreeBoard(board);
          //TODO make file ReportError(MEM_OUT);
          return NULL;
      }
  }
  
  static char* allocateBoardArray(int row, int col) {
      char* newBoard = (char*) malloc(row * col * sizeof(char));
  
      if (newBoard == NULL) {
          return NULL;
      }
      return newBoard;
  }
  </code></pre>
  
  <p>this is BoardP:</p>
  
  <pre><code>typedef struct Board* BoardP;
  </code></pre>