在NSOperation中释放对象

时间:2011-09-21 15:04:36

标签: objective-c memory-management

我已经将NSOperation子类化,并在需要时将其实例添加到NSOperationQue。 在NSOperation的主要功能中,我创建了一个AudioBufferList,然后在我完成时释放它。

在乐器的内存分配部分,它显示这些AudioBufferLists的实例不断攀升。生存的字节数不断攀升,缓冲区的mdata部分似乎是罪魁祸首。

这是我的代码的一部分。

我是否正确发布了缓冲区列表,或者仪器报告不正确?

-(void)main

{
//autorelase stuff in here



        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 


  AudioBufferList *bufferList;

            bufferList = (AudioBufferList *) malloc (
                                                     sizeof (AudioBufferList) + sizeof (AudioBuffer) * (1)
                                                     );

            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]

}

1 个答案:

答案 0 :(得分:1)

你的mData是罪魁祸首,请确保你将它们分配为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];