I'm reading an audio file into RAM using the following method by passing an MPMediaItem object. However, each time this is called, the beginning of the function is supposed to free the memory used for the previous audio file and it doesn't, i.e., Xcode shows the memory usage keeps increasing each time the function is called and eventually the OS runs out of memory.
Am I doing anything obviously wrong?
NOTE: the below function is the only place I am using my audioData ivar
void *audioData;
- (void)playItem:(MPMediaItem *)mediaItem
{
if (audioData != NULL) {
NSLog(@"audio data pointer not empty");
free (audioData);
}
// open audio file
AudioFileID audioFileID;
NSURL *mediaItemURL = [mediaItem valueForProperty:MPMediaItemPropertyAssetURL];
OSErr err;
err = AudioFileOpenURL((__bridge CFURLRef)mediaItemURL,
kAudioFileReadPermission,
0,
&audioFileID);
NSAssert(err == noErr, @"Couldn't open audio file...");
// read audio file properties and malloc audioData property
UInt32 audioDataPacketCount;
UInt32 audioDataPacketCountSize = sizeof(audioDataPacketCount);
err = AudioFileGetProperty(audioFileID,
kAudioFilePropertyAudioDataPacketCount,
&audioDataPacketCountSize,
&audioDataPacketCount);
NSAssert(err == noErr, @"Couldn't read audio file properties...");
UInt32 audioDataByteCount;
UInt32 audioDataByteCountSize = sizeof(audioDataByteCount);
err = AudioFileGetProperty(audioFileID,
kAudioFilePropertyAudioDataByteCount,
&audioDataByteCountSize,
&audioDataByteCount);
NSAssert(err == noErr, @"Couldn't read audio file properties...");
audioData = malloc(audioDataByteCount);
// read audio file packets
err = AudioFileReadPacketData(audioFileID,
false,
&audioDataByteCount,
NULL,
0,
&audioDataPacketCount,
audioData);
NSAssert(err == noErr, @"Couldn't read audio file to memory...");
}
EDIT: I used the same code structure and read the audio file data using ExtAudioFileRead function instead and memory deallocates and gets reallocated as supposedly. It may be something to do with how I'm using the AudioFileReadPacketData function =/