7

我有一些代码可以创建 CMBlockBuffers,然后创建一个 CMSampleBuffer 并将其传递给 AVAssetWriterInput。

这里的内存管理有什么问题?根据 Apple 文档,您在名称中使用“Create”的任何内容都应使用 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在任何东西上使用吗?

4

2 回答 2

9

关键是 CMBlockBufferCreateWithMemoryBlock 的 blockAllocator 参数:

如果 memoryBlock 为 NULL,则分配器用于分配 memoryBlock。如果 memoryBlock 不为​​ NULL,则此分配器将用于释放它(如果提供)。传递 NULL 将导致使用默认分配器(在调用时设置)。如果不需要解除分配,则传递 kCFAllocatorNull。

由于您不希望在释放 CMBlockBuffer 时释放“样本”,因此您希望将 kCFAllocatorNull 作为第 4 个参数传入,如下所示:

status = CMBlockBufferCreateWithMemoryBlock(
                                            kCFAllocatorDefault, 
                                            samples, 
                                            buflen, 
                                            kCFAllocatorNull, 
                                            NULL, 
                                            0, 
                                            buflen, 
                                            0, 
                                            &tmp_bbuf);
于 2014-02-21T19:31:46.347 回答
0
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);
于 2013-06-25T03:24:56.817 回答