title: NSString stringWithCString:encoding: - not copying the Cstring?
body: <p>I need to compute a signature (a kind of a hash) for my object. The computation requires many per-character operations, so to speed this procedure up, my code operates on CString, and after that converts computed CString to final NSString. The stub code looks like this:</p>
  
  <pre><code>- (NSString *)signature
  {
      NSString *signatureString = @"?";
      char *buffer;
  
      buffer = malloc(sizeof(char)*(self.hashLength+1));
      if ( buffer ) {
          // Code computing a signature into the buffer
  
          signatureString = [NSString stringWithCString:buffer
                                               encoding:NSASCIIStringEncoding];
          free(buffer);
      }
  
      return signatureString;
  }
  </code></pre>
  
  <p>I expected that upon creation of NSString characters from source CString are copied into internal structure of NSString object. However in runtime I am getting the error:</p>
  
  <pre><code>malloc: *** error for object 0x8f734d4: incorrect checksum for freed object - object was probably modified after being freed.
  *** set a breakpoint in malloc_error_break to debug
  </code></pre>
  
  <p>This looks like chars are not copied, but used from their original memory location. Is it really so?
  Does it mean that I need to not free allocated memory?
  Will the NSString deallocate that CString memory upon object destruction?</p>
  
  <p>I can not find any documentation explaining that issue, but because I want to avoid memory leak obviously, I would prefer to be sure, </p>