CMBlockBuffer Save Memory

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

What is the memory management deal here? According to Apple documentation, everything you use with 'Create' in a name must be released with CFRelease .

However, if I use CFRelease, my application fails with a malloc: * error for object 0xblahblah: the freed pointer 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, should CFRelease use CFRelease in this code?

+6
objective-c iphone avfoundation core-media
source share
2 answers

The key is the blockAllocator parameter for CMBlockBufferCreateWithMemoryBlock:

An allocator that will be used to allocate a block of memory if memoryBlock is ZERO. If memoryBlock is not NULL, this allocator will be used to free it, if provided. Passing NULL will result in a default allocator (as set during the call) to be used. Pass kCFAllocatorNull if no release is required.

Since you do not want the "samples" to be freed when CMBlockBuffer is freed, you want to pass kCFAllocatorNull as the 4th parameter, for example:

 status = CMBlockBufferCreateWithMemoryBlock( kCFAllocatorDefault, samples, buflen, kCFAllocatorNull, NULL, 0, buflen, 0, &tmp_bbuf); 
+8
source share
 CMAudioSampleBufferCreateWithPacketDescriptions(kCFAllocatorDefault, bbuf, TRUE, 0, NULL, audio_fmt_desc_, 1, timestamp, NULL, &sbuf); 

it should be:

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

All Articles