title: malloc: Invalid pointer dequeued from free list
body: <p>I have some C++ code in my OS X project that allocates an array thusly:</p>
  
  <pre><code>C * p = new C[lengthHint + 2];
  </code></pre>
  
  <p>This is in a template class; <code>C</code> is <code>unsigned short</code>. <code>lengthHint</code> is 1. This is all irrelevant. The error I get at runtime is:</p>
  
  <pre><code>malloc: *** error for object 0x60800000c4f0: Invalid pointer dequeued from free list
  *** set a breakpoint in malloc_error_break to debug
  </code></pre>
  
  <p>It appears <code>malloc</code> is failing because a previous call to <code>free</code> freed something that wasn't valid. But it seems like <code>free</code> would've complained about that at the time.</p>
  
  <p>Obviously there are millions of <code>malloc/free</code> and <code>new/delete</code> calls being executed and this same code is running without issues in other programs running on iOS and OS X. I'm not sure how to approach debugging this and am looking for suggestions.</p>