title: ExtAudioFileWrite to m4a/aac failing on dual-core devices (ipad 2, iphone 4s)
body: <p>I wrote a loop to encode pcm audio data generated by my app to aac using Extended Audio File Services. The encoding takes place in a background thread synchronously, and not in real-time.</p>
  
  <p>The encoding works flawlessly on ipad 1 and iphone 3gs/4 for both ios 4 and 5. However, for dual-core devices (iphone 4s, ipad 2) the third call to ExtAudioFileWrite crashes the encoding thread with no stack trace and no error code.</p>
  
  <p>Here is the code in question:</p>
  
  <p>The data formats</p>
  
  <pre><code>AudioStreamBasicDescription AUCanonicalASBD(Float64 sampleRate, 
                                          UInt32 channel){
  AudioStreamBasicDescription audioFormat;
  audioFormat.mSampleRate         = sampleRate;
  audioFormat.mFormatID           = kAudioFormatLinearPCM;
  audioFormat.mFormatFlags        = kAudioFormatFlagsAudioUnitCanonical;
  audioFormat.mChannelsPerFrame   = channel;
  audioFormat.mBytesPerPacket     = sizeof(AudioUnitSampleType);
  audioFormat.mBytesPerFrame      = sizeof(AudioUnitSampleType);
  audioFormat.mFramesPerPacket    = 1;
  audioFormat.mBitsPerChannel     = 8 * sizeof(AudioUnitSampleType);
  audioFormat.mReserved           = 0;
  return audioFormat;
  }
  
  AudioStreamBasicDescription MixdownAAC(void){
  AudioStreamBasicDescription audioFormat;
  audioFormat.mSampleRate         = 44100.0;
  audioFormat.mFormatID           = kAudioFormatMPEG4AAC;
  audioFormat.mFormatFlags        = kMPEG4Object_AAC_Main;
  audioFormat.mChannelsPerFrame   = 2;
  audioFormat.mBytesPerPacket     = 0;
  audioFormat.mBytesPerFrame      = 0;
  audioFormat.mFramesPerPacket    = 1024;
  audioFormat.mBitsPerChannel     = 0;
  audioFormat.mReserved           = 0;
  return audioFormat;
  }
  </code></pre>
  
  <p>The render loop</p>
  
  <pre><code>OSStatus err;
  ExtAudioFileRef outFile;
  NSURL *mixdownURL = [NSURL fileURLWithPath:filePath isDirectory:NO];
  
  // internal data format
  AudioStreamBasicDescription localFormat = AUCanonicalASBD(44100.0, 2);
  
  // output file format
  AudioStreamBasicDescription mixdownFormat = MixdownAAC();
  err = ExtAudioFileCreateWithURL((CFURLRef)mixdownURL,
                               kAudioFileM4AType,
                               &amp;mixdownFormat, 
                               NULL,
                               kAudioFileFlags_EraseFile,
                               &amp;outFile);
  
  
  err = ExtAudioFileSetProperty(outFile, kExtAudioFileProperty_ClientDataFormat, sizeof(AudioStreamBasicDescription), &amp;localFormat);
  
  // prep
  AllRenderData *allData = &amp;allRenderData;
  writeBuffer = malloc(sizeof(AudioBufferList) + (2*sizeof(AudioBuffer)));
  writeBuffer-&gt;mNumberBuffers = 2;
  writeBuffer-&gt;mBuffers[0].mNumberChannels = 1;
  writeBuffer-&gt;mBuffers[0].mDataByteSize = bufferBytes;
  writeBuffer-&gt;mBuffers[0].mData = malloc(bufferBytes);
  writeBuffer-&gt;mBuffers[1].mNumberChannels = 1;
  writeBuffer-&gt;mBuffers[1].mDataByteSize = bufferBytes;
  writeBuffer-&gt;mBuffers[1].mData = malloc(bufferBytes);
  
  memset(writeBuffer-&gt;mBuffers[0].mData, 0, bufferBytes);
  memset(writeBuffer-&gt;mBuffers[1].mData, 0, bufferBytes);
  
  UInt32 framesToGet;
  UInt32 frameCount = allData-&gt;gLoopStartFrame;
  UInt32 startFrame = allData-&gt;gLoopStartFrame;
  UInt32 lastFrame = allData-&gt;gLoopEndFrame;
  
  // write one silent buffer
  ExtAudioFileWrite(outFile, bufferFrames, writeBuffer);
  
  while (frameCount &lt; lastFrame){
  
      // how many frames do we need to get
      if (lastFrame - frameCount &gt; bufferFrames)
          framesToGet = bufferFrames;
      else
          framesToGet = lastFrame - frameCount;
  
      // get dem frames
      err = theBigOlCallback((void*)&amp;allRenderData,
                              NULL, NULL, 1,
                             framesToGet, writeBuffer);
  
      // write to output file
      ExtAudioFileWrite(outFile, framesToGet, writeBuffer);
  
      frameCount += framesToGet;
  }
  
  // write one trailing silent buffer
  memset(writeBuffer-&gt;mBuffers[0].mData, 0, bufferBytes);
  memset(writeBuffer-&gt;mBuffers[1].mData, 0, bufferBytes);
  processLimiterInPlace8p24(limiter, writeBuffer-&gt;mBuffers[0].mData, writeBuffer-&gt;mBuffers[1].mData, bufferFrames);
  ExtAudioFileWrite(outFile, bufferFrames, writeBuffer);
  
  err = ExtAudioFileDispose(outFile);
  </code></pre>
  
  <p>The pcm frames are properly created, but ExtAudioFileWrite fails the 2nd/3rd time it is called.</p>
  
  <p>Any ideas? Thank you!</p>