首页 > 解决方案 > 视频工具箱。像素传输,何时释放源缓冲区?

问题描述

一个简单的直接问题,传输图像后何时释放源像素缓冲区以避免崩溃:

//pixel_buffer is the original 
CVPixelBufferCreate(kCFAllocatorDefault, 
     CVPixelBufferGetWidth(pixel_buffer), 
     CVPixelBufferGetHeight(pixel_buffer), 
     kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, 
     NULL, &targetPxb);

if (targetPxb != NULL) {
     auto status = VTPixelTransferSessionTransferImage(transSession, 
                           pixel_buffer, 
                           targetPxb);
     if (status == noErr) {
           //  CFRelease(pixel_buffer); //this will cause crash
     }
}

标签: video-toolbox

解决方案


检查pixel_buffer的引用计数。如果你没有添加 CFRetain,执行 CFRelease 会导致你的应用程序崩溃,因为引用计数已经是 0,所以在这种情况下不需要调用 CFRelease。有一些简单的方法可以检查引用计数,例如:

CFGetRetainCount

推荐阅读