title: Heap corruption error with delete []
body: <p>So I'm a pretty big new at C++, so I'm sure this is a relatively simple problem, but I have a legacy C++ app I'm trying to trace a heap corruption problem and have traced it to this function:</p>
  
  <pre><code>void LTrimZeros(CString *pstr)
  {
      char *psz1;
      char *psz2;
  
      if ( pstr-&gt;GetLength() == 0 )
          return;
  
  
      psz1 = new char[pstr-&gt;GetLength() + 1];
      psz2 = psz1;
  
      strcpy_s( psz2, strlen(psz2), (const char *) *pstr );
  
      while ( *psz2 == '0' )
      {
          psz2++;
      }
  
      *pstr = psz2;
  
      delete [] psz1;
  
      return;
  }
  </code></pre>
  
  <p>When it tries to delete <code>psz1</code> it throws a heap corruption error.  Again I am pretty new to C++, so I didn't want to try to fix this and accidentally introduce a memory leak, so I thought I'd ask the experts.  Alternative solutions of the same function are also fine, as this app was written in like c++4 originally, but now is upgraded to c++11 (Also a brief explanation of why this causes heap corruption would help a lot).</p>