CMBlockBufferCreate memory management
Asked Answered
S

2

7

I have some code that creates CMBlockBuffers and then creates a CMSampleBuffer and passes it to an AVAssetWriterInput.

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.

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);

So, in this code, should I be using CFRelease on anything?

Snowmobile answered 1/9, 2012 at 12:18 Comment(2)
Did you ever get this figured out?Goldcrest
@Goldcrest I ended up modifying it all to do asynchronous compression using GCD. I did need to free those buffers. I'm still not sure why this was crashing. If you fear that you have memory leaks, profile your code using Instruments (part of XCode).Snowmobile
N
9

The key is the blockAllocator parameter to CMBlockBufferCreateWithMemoryBlock:

Allocator to be used for allocating the memoryBlock, if memoryBlock is NULL. If memoryBlock is non-NULL, this allocator will be used to deallocate it if provided. Passing NULL will cause the default allocator (as set at the time of the call) to be used. Pass kCFAllocatorNull if no deallocation is desired.

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);
Neoclassicism answered 21/2, 2014 at 19:31 Comment(0)
S
0
CMAudioSampleBufferCreateWithPacketDescriptions(kCFAllocatorDefault, bbuf, TRUE, 0, NULL, audio_fmt_desc_, 1, timestamp, NULL, &sbuf);

this should be:

CMAudioSampleBufferCreateWithPacketDescriptions(kCFAllocatorDefault, bbuf, TRUE, 0, NULL, audio_fmt_desc_, 1024, timestamp, NULL, &sbuf);
Symbol answered 25/6, 2013 at 3:24 Comment(1)
Care to expand upon why this relates to memory management and CFRelease?Snowmobile

© 2022 - 2024 — McMap. All rights reserved.