Releasing Objects In an NSOperation
I've subclassed NSOperation and add instances of it to an NSOperationQue when I need it. In the main function of the NSOperation I create an AudioBufferList and then free it when I'm done.
In the memory allocation section of instruments it's showing that instances of these AudioBufferLists are constantly climbing. The living number of bytes constantly climbs and the mdata part of the buffer appears to be the culprit.
This is a segment of my code.
Am I correctly releasing my bufferlist or could instruments be reporting incorrectly?
-(void)main
{
//autorelase stuff in here
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
AudioBufferList *bufferList;
bufferList = (AudioBufferList *) malloc (
sizeof (AudioBufferList) + sizeof (AudioBuffer) * (1开发者_Go百科)
);
if (NULL == bufferList) {NSLog (@"*** malloc failure for allocating bufferList memory"); return;}
// initialize the mNumberBuffers member
bufferList->mNumberBuffers = 2;
// initialize the mBuffers member to 0
AudioBuffer emptyBuffer = {0};
size_t arrayIndex;
for (arrayIndex = 0; arrayIndex < 2; arrayIndex++) {
bufferList->mBuffers[arrayIndex] = emptyBuffer;
}
// set up the AudioBuffer structs in the buffer list
bufferList->mBuffers[0].mNumberChannels = 1;
bufferList->mBuffers[0].mDataByteSize = numberofframestoread * sizeof (AudioUnitSampleType);
bufferList->mBuffers[0].mData = (AudioUnitSampleType*)calloc(numberofframestoread, sizeof(AudioUnitSampleType));
if (2 == 2) {
bufferList->mBuffers[1].mNumberChannels = 1;
bufferList->mBuffers[1].mDataByteSize = numberofframestoread * sizeof (AudioUnitSampleType);
bufferList->mBuffers[1].mData = (AudioUnitSampleType*)calloc(numberofframestoread, sizeof(AudioUnitSampleType));
}
AudioUnitSampleType *inSamplesChannelLeft=bufferList->mBuffers[0].mData;
AudioUnitSampleType *inSamplesChannelRight=bufferList->mBuffers[1].mData;
// do stuff with buffer here
free(bufferList);
bufferlist=nil;
[pool release]
}
Your mData
is the culprit, make sure you free those as well since you allocate them as calloc.
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
const size_t nBuffers = 2;
AudioBufferList *bufferList;
bufferList = (AudioBufferList *) malloc (
sizeof (AudioBufferList) + sizeof (AudioBuffer) * (nBuffers - 1)
);
if (NULL == bufferList) {NSLog (@"*** malloc failure for allocating bufferList memory"); [pool drain]; return;}
// initialize the mNumberBuffers member
bufferList->mNumberBuffers = nBuffers;
//...
bufferList->mBuffers[0].mData = (AudioUnitSampleType*)calloc(numberofframestoread, sizeof(AudioUnitSampleType));
//...
bufferList->mBuffers[1].mData = (AudioUnitSampleType*)calloc(numberofframestoread, sizeof(AudioUnitSampleType));
//...
//Free mData here
for(size_t i = 0; i < nBuffers; i++)
free(bufferList->mBuffers[i].mData);
free(bufferList);
[pool drain];
精彩评论