我正在使用Manatee Barcode Scanner SDK,它要求我编写此代码来解码捕获的图像:
unsigned char *frameBuffer = malloc(width * height);
memcpy(frameBuffer, baseAddress, width * height);
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
unsigned char *pResult=NULL;
int resLength = MWB_scanGrayscaleImage(frameBuffer,width,height, &pResult);
free(frameBuffer);
...
});
由于某种原因,free(frameBuffer)
调用没有释放内存,我有内存泄漏。运行几个小时后,应用程序崩溃并出现内存错误。
在仪器中我可以看到内存泄漏:
这可能是一个问题,因为我试图在一个块中使用free()
吗?或者因为它在另一个线程中运行?
我尝试过像这样添加__block
:
__block unsigned char *frameBuffer = malloc(width * height);
但它没有帮助。
我尝试在块中移动前3行代码:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
unsigned char *frameBuffer = malloc(width * height);
memcpy(frameBuffer, baseAddress, width * height);
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
unsigned char *pResult=NULL;
int resLength = MWB_scanGrayscaleImage(frameBuffer,width,height, &pResult);
free(frameBuffer);
});
但这给了我CVPixelBufferUnlockBaseAddress(imageBuffer,0);
请帮我解决这个问题。感谢。