CMBlockBufferCreate内存管理 [英] CMBlockBufferCreate memory management
问题描述
我有一些代码可以创建CMBlockBuffers,然后创建一个CMSampleBuffer并将其传递给AVAssetWriterInput。
I have some code that creates CMBlockBuffers and then creates a CMSampleBuffer and passes it to an AVAssetWriterInput.
这里的内存管理有什么优惠?根据Apple文档,任何内容你使用名称中的'Create'应该与CFRelease一起发布。
What's the deal on memory management here? According to the Apple documentation, anything you use with 'Create' in the name should be released with CFRelease.
但是,如果我使用CFRelease,那么我的应用程序将以'malloc: * 错误:未分配对象被释放。
However, if I use CFRelease then my app aborts with 'malloc: * error for object 0xblahblah: pointer being freed was not allocated.
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
关于什么?
So, in this code, should I be using CFRelease
on anything?
推荐答案
关键是CMBlockBufferCreateWithMemoryBlock的blockAllocator参数:
The key is the blockAllocator parameter to CMBlockBufferCreateWithMemoryBlock:
用于分配memoryBlock的分配器,如果memoryBlock是
NULL。如果memoryBlock为非NULL,则此分配器将用于
解除分配(如果提供)。传递NULL将导致使用默认的
分配器(在调用时设置)。 如果不需要重新分配,则传递
kCFAllocatorNull。
因为你不想要示例当你释放CMBlockBuffer时要解除分配,你想传入kCFAllocatorNull作为第四个参数,如下所示:
Since you don't want the "samples" to be deallocated when you release the CMBlockBuffer, you want to pass in kCFAllocatorNull as the 4th parameter like so:
status = CMBlockBufferCreateWithMemoryBlock(
kCFAllocatorDefault,
samples,
buflen,
kCFAllocatorNull,
NULL,
0,
buflen,
0,
&tmp_bbuf);
这篇关于CMBlockBufferCreate内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!