所以我在转换某些音频方面遇到了问题。我在转换时遇到了巨大的内存泄漏,它来自这些代码行。
dispatch_queue_t mediaInputQueue = dispatch_queue_create("mediaInputQueue", NULL);
[assetWriterInput requestMediaDataWhenReadyOnQueue:mediaInputQueue
usingBlock: ^
{
while (assetWriterInput.readyForMoreMediaData)
{
CMSampleBufferRef nextBuffer = [assetReaderOutput copyNextSampleBuffer];
if (nextBuffer)
{
[assetWriterInput appendSampleBuffer: nextBuffer];
nextBuffer = NULL;
}
else
{
// done!
[assetWriterInput markAsFinished];
[assetWriter finishWriting];
[assetReader cancelReading];
NSDictionary *outputFileAttributes = [[NSFileManager defaultManager]
attributesOfItemAtPath:exportPath
error:nil];
NSNumber *doneFileSize = [NSNumber numberWithLong:[outputFileAttributes fileSize]];
[self performSelectorOnMainThread:@selector(updateCompletedSizeLabel:)
withObject:doneFileSize
waitUntilDone:NO];
// release a lot of stuff
[assetReader release];
[assetReaderOutput release];
[assetWriter release];
[assetWriterInput release];
[exportPath release];
break;
}
}
}];
似乎导致泄漏的线是: CMSampleBufferRef nextBuffer = [assetReaderOutput copyNextSampleBuffer];
我迷失了这一点,任何帮助都会非常感激。
解答: 修复 - 只需在if / else语句的末尾添加这些行。
CMSampleBufferInvalidate(nextBuffer);
CFRelease(nextBuffer);
nextBuffer = nil; // NULL?
答案 0 :(得分:12)
答案:已修复 - 只需在if / else语句的末尾添加这些行。
CMSampleBufferInvalidate(nextBuffer);
CFRelease(nextBuffer);
nextBuffer = NULL;