How to create CMBlockBufferRef from NSData
I am struggling (getting memory errors or apparently not freeing memory correctly) trying to create a CMBlockBufferRef filled with data from existing NSData (or NSMutableData). I would be happy with a solution that copies data, but ideally I would look at a solution that will use the base NSData bytes and hold a strong reference to the NSData object until the CMBlockBuffer is freed.
+3
source to share
1 answer
For read-only buffer referencing NSData
(no copying, of course), I just found a way to achieve it.
static void releaseNSData(void *o, void *block, size_t size)
{
NSData *data = (__bridge_transfer NSData*) o;
data = nil; // Assuming ARC is enabled
}
OSStatus createReadonlyBlockBuffer(CMBlockBufferRef *result, NSData *data)
{
CMBlockBufferCustomBlockSource blockSource =
{
.version = kCMBlockBufferCustomBlockSourceVersion,
.AllocateBlock = NULL,
.FreeBlock = &releaseNSData,
.refCon = (__bridge_retained void*) data,
};
return CMBlockBufferCreateWithMemoryBlock(NULL, (uint8_t*) data.bytes, data.length, NULL, &blockSource, 0, data.length, 0, result);
}
+3
source to share