gpt4 book ai didi

objective-c - 何时释放CMBlockBufferRef?

转载 作者:行者123 更新时间:2023-12-01 19:19:11 24 4
gpt4 key购买 nike

我正在通过AV Foundation的导出功能从iPod库中的轨道读取LPCM样本。我的大部分代码都是从Chris Adamson's example here借用的。

我正在设置一个新的CMBlockBufferRef,并使用CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer保留它。根据苹果的CFType reference,我需要在我明确保留的任何对象上使用CFRelease

问题是我似乎无法弄清楚在哪里释放CMBlockBufferRef对象。如果我根本不释放它,则会出现大量内存泄漏。如果我尝试在循环内的任何地方释放它,则该应用程序崩溃,并且GrabAudioData方法中出现EXC_BAD_ACCESS错误。如果我尝试在循环外启动并释放它,则内存泄漏仍然存在。我试过测试,看看blockBuffer是否是NULL,然后再发布,但这不起作用。请帮忙!

while (assetReader.status != AVAssetReaderStatusCompleted)
{
if (assetReader.status == AVAssetReaderStatusReading)
{
CMSampleBufferRef nextBuffer = [readerOutput copyNextSampleBuffer];

if (nextBuffer)
{
AudioBufferList bufferList;
CMBlockBufferRef blockBuffer;
CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(nextBuffer, NULL, &bufferList, sizeof(bufferList), NULL, NULL, kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment, &blockBuffer);

// Using the AudioBufferList
bufferManager->GrabAudioData(&bufferList);

CMSampleBufferInvalidate(nextBuffer);
CFRelease(nextBuffer);

// Releasing here causes a crash
// CFRelease(blockBuffer);
}
else
{
break;
}
}
}

最佳答案

除了@ sinn246的答案,您还必须检查CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer的返回值:

OSStatus err = CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(nextBuffer, NULL, &bufferList, sizeof(bufferList), NULL, NULL, kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment, &blockBuffer);

if (!err && blockBuffer && bufferList.mBuffers[0].mData && (bufferList.mBuffers[0].mDataByteSize > 0))
{
// Using the AudioBufferList
bufferManager->GrabAudioData(&bufferList);
}

if (blockBuffer) // Double check that what you are releasing actually exists!
{
CFRelease(blockBuffer);
}

CMSampleBufferInvalidate(nextBuffer);
CFRelease(nextBuffer);

关于objective-c - 何时释放CMBlockBufferRef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11143359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com