CMBlockBufferCreate内存管理(CMBlockBufferCreate memory

2019-07-31 07:21发布

我有一个创建CMBlockBuffers,然后创建一个CMSampleBuffer并把它传递给AVAssetWriterInput一些代码。

什么是内存管理这里的交易吗? 根据苹果的文档, 你的名字“创建”任何使用应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上什么?

Answer 1:

关键是blockAllocator参数CMBlockBufferCreateWithMemoryBlock:

用于分配MemoryBlock中分配,如果MemoryBlock中为NULL。 如果MemoryBlock中的非NULL,该分配器将用于如果提供来释放它。 传递NULL将导致默认分配器(如设定在通话时)使用。 如果没有释放需要通过kCFAllocatorNull。

既然你不想要的“样本”当你释放CMBlockBuffer被释放吗,你想在kCFAllocatorNull通过像这样的第四个参数:

status = CMBlockBufferCreateWithMemoryBlock(
                                            kCFAllocatorDefault, 
                                            samples, 
                                            buflen, 
                                            kCFAllocatorNull, 
                                            NULL, 
                                            0, 
                                            buflen, 
                                            0, 
                                            &tmp_bbuf);


Answer 2:

CMAudioSampleBufferCreateWithPacketDescriptions(kCFAllocatorDefault, bbuf, TRUE, 0, NULL, audio_fmt_desc_, 1, timestamp, NULL, &sbuf);

这应该是:

CMAudioSampleBufferCreateWithPacketDescriptions(kCFAllocatorDefault, bbuf, TRUE, 0, NULL, audio_fmt_desc_, 1024, timestamp, NULL, &sbuf);


文章来源: CMBlockBufferCreate memory management