title: incorrect checksum for freed object
body: <p>I am getting this error (memory location varies between runs):</p>
  
  <pre><code>q2(4910,0x7fff7a1d4300) malloc: *** error for object 0x7fdf79c04bd8: incorrect checksum for freed object - object was probably modified after being freed.
  *** set a breakpoint in malloc_error_break to debug
  Abort trap: 6
  </code></pre>
  
  <p>This is the function that crashes:</p>
  
  <pre><code>public:
  // construct a 'rows X cols' matrix.
  SMatrix(int rows, int cols) {
      if (rows&lt;1 || cols&lt;1) {
          cout&lt;&lt;"Invalid row/col value(s).";
          exit(-1);
      }
      this-&gt;_rows = rows;
      this-&gt;_cols = cols;
      this-&gt;_vertical = new simpleNode [rows];
      this-&gt;_horizontal = new simpleNode [cols];
      if (this-&gt;_vertical == NULL || this-&gt;_horizontal==NULL) {
          cout&lt;&lt;"Exiting";
          exit(-1);
      }
      initArrays();
  }
  </code></pre>
  
  <p>It crashes on this particular line:</p>
  
  <pre><code>  this-&gt;_horizontal = new simpleNode [cols];
  </code></pre>
  
  <p>The function that calls:</p>
  
  <pre><code>int main() {
        SMatrix bigM(500,500);
        bigM.setElement(10,20,17);
        cout &lt;&lt;" bigM - total size in bytes: (implementation depended): "
         &lt;&lt; bigM.sizeInBytes() &lt;&lt; endl &lt;&lt; endl; 
  
        SMatrix m1(7,10),m2(7,10),m4(10,2),m5(7,2); //Crashes on m4(10,2)
  }
  </code></pre>
  
  <p>Other functions that could be relevant:</p>
  
  <pre><code>struct simpleNode {
      Node* _next;
  };
  int _rows; //Number of rows in this SMatrix
  int _cols; //Number of columns in this SMatrix
  simpleNode * _vertical; //array (simpleNode)
  simpleNode * _horizontal;  //array (simpleNode)
  /*Initiate the horizontal/vertical arrays to point to null*/
  void initArrays() {
      int i;
      for (i=0; i&lt;this-&gt;_rows; i++)
          this-&gt;_horizontal[i]._next = NULL;
      for (i=0; i&lt;this-&gt;_cols; i++)
          this-&gt;_vertical[i]._next = NULL;
  }
  </code></pre>
  
  <p>I am on OSX. I compiled with -g and ran it with GDB but <em>Program exited normally.</em>
  How can I debug this if I don't use XCode? Also a hint on how to fix the problem would be very helpful.</p>
  
  <p>Edit: I'm running the output file and sometimes it runs while others it gives me the error. Seems to be at a random order. Also, the program never fails when I run it on gdb it always exits correctly. Why is this happening?</p>