title: malloc error - incorrect checksum for freed object - object was probably modified after being freed
body: <p>I'm trying to get sub data of NSData object and at the same time multiple bytes by some value for my personal need  . </p>
  
  <p>actually this affect the volume of .wav sound file .</p>
  
  <p>but i get after few calls to the following function a malloc error at the malloc statement . </p>
  
  <pre><code>+(NSData *) subDataOfData: (NSData *) mainData withRange:(NSRange) range volume (CGFloat) volume
  {
      // here is the problematic line:
      Byte * soundWithVolumeBytes = (Byte*)malloc(range.length); 
      Byte * mainSoundFileBytes =(Byte *)[mainData bytes];
  
      for (int i=range.location ; i&lt; range.location + range.length; i=i+2)
      {
          // get the original sample
          int16_t sampleInt16Value = 0;
          sampleInt16Value = (sampleInt16Value&lt;&lt;8) + mainSoundFileBytes[i+1];
          sampleInt16Value = (sampleInt16Value&lt;&lt;8) + mainSoundFileBytes[i];
  
          //multiple sample 
          sampleInt16Value*=volume;
  
          //store the sample
          soundWithVolumeBytes[i] = (Byte)sampleInt16Value;
          soundWithVolumeBytes[i+1] =(Byte) (sampleInt16Value&gt;&gt;8);
  
      }
  
  
      NSData * soundDataWithVolume = [[NSData alloc] initWithBytes:soundWithVolumeBytes length:range.length];
      free(soundWithVolumeBytes);
  
      return [soundDataWithVolume autorelease];
  
  }
  </code></pre>
  
  <p>Thanks !!</p>