【发布时间】:2012-08-26 23:55:51
【问题描述】:
我有一些代码创建 CMBlockBuffers,然后创建一个 CMSampleBuffer 并将其传递给 AVAssetWriterInput。
这里的内存管理有什么问题?根据 Apple 文档,anything you use with 'Create' in the name should be released with CFRelease。
但是,如果我使用 CFRelease,那么我的应用程序会因 'malloc: * 对象 0xblahblah 错误:未分配指针被释放而中止。
CMBlockBufferRef tmp_bbuf = NULL;
CMBlockBufferRef bbuf = NULL;
CMSampleBufferRef sbuf = NULL;
status = CMBlockBufferCreateWithMemoryBlock(
kCFAllocatorDefault,
samples,
buflen,
kCFAllocatorDefault,
NULL,
0,
buflen,
0,
&tmp_bbuf);
if (status != noErr || !tmp_bbuf) {
NSLog(@"CMBlockBufferCreateWithMemoryBlock error");
return -1;
}
// Copy the buffer so that we get a copy of the samples in memory.
// CMBlockBufferCreateWithMemoryBlock does not actually copy the data!
//
status = CMBlockBufferCreateContiguous(kCFAllocatorDefault, tmp_bbuf, kCFAllocatorDefault, NULL, 0, buflen, kCMBlockBufferAlwaysCopyDataFlag, &bbuf);
//CFRelease(tmp_bbuf); // causes abort?!
if (status != noErr) {
NSLog(@"CMBlockBufferCreateContiguous error");
//CFRelease(bbuf);
return -1;
}
CMTime timestamp = CMTimeMake(sample_position_, 44100);
status = CMAudioSampleBufferCreateWithPacketDescriptions(
kCFAllocatorDefault, bbuf, TRUE, 0, NULL, audio_fmt_desc_, 1, timestamp, NULL, &sbuf);
sample_position_ += n;
if (status != noErr) {
NSLog(@"CMSampleBufferCreate error");
return -1;
}
BOOL r = [audioWriterInput appendSampleBuffer:sbuf]; // AVAssetWriterInput
//memset(&audio_buf_[0], 0, buflen);
if (!r) {
NSLog(@"appendSampleBuffer error");
}
//CFRelease(bbuf);
//CFRelease(sbuf);
那么,在这段代码中,我是否应该在任何东西上使用CFRelease?
【问题讨论】:
-
你有想过这个问题吗?
-
@kevlar 我最终将其全部修改为使用 GCD 进行异步压缩。我确实需要释放这些缓冲区。我仍然不确定为什么会崩溃。如果您担心内存泄漏,请使用 Instruments(XCode 的一部分)分析您的代码。
标签: iphone objective-c avfoundation core-media