iosmemory-managementcgbitmapcontext

CGBitmapContextCreate memory release responsibility


This is how I create a UIImage. I allocated piOutData with calloc. Now when I call CGImageRelease at the end, will it automatically release piOutData or I need to do it manually via free.

int m_iH = iInMaxDim;
int m_iW = iInMaxDim;

UInt8*piOutData = calloc(iInMaxDim *iInMaxDim*4,sizeof(UInt8));
CGContextRef ctx = CGBitmapContextCreate(piOutData,  
                                         m_iW,  
                                         m_iH,  
                                             CGImageGetBitsPerComponent(inImage.CGImage),
                                         m_iW*4,  
                                         CGImageGetColorSpace(inImage.CGImage),  
                                         CGImageGetBitmapInfo(inImage.CGImage) 
                                         ); 
CGImageRef imageRef = CGBitmapContextCreateImage(ctx);  
CGContextRelease(ctx);
UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

Solution

  • …when I call CGImageRelease at the end, will it automatically release piOutData?

    No. piOutData is owned by you.