Search code examples
objective-copengl-esmemory-leakssparrow-framework

Where is the memory leak in this OpneGL-ES Screenshot function used on the IPad?


i am using this function to create Screenshots of the my IPAD App. I am using the Sparrow Framework in my Project. SPDisplayObject uses OpenGl-ES based rendering.

@implementation SPDisplayObject (ScreenshotFromSPDisplayObject)

- (UIImage *)getImageScreenshot{

int WIDTH = 1024;
int HEIGHT = 768;

CGSize size = CGSizeMake(WIDTH,HEIGHT);
//Create un buffer for pixels
GLuint bufferLenght=size.width*size.height*4;
GLubyte *buffer = (GLubyte *) malloc(bufferLenght);

//Read Pixels from OpenGL
glReadPixels(0,0,size.width,size.height,GL_RGBA,GL_UNSIGNED_BYTE,buffer);
//Make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, bufferLenght, NULL);

//Configure image
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * size.width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef iref = CGImageCreate(size.width,size.height,bitsPerComponent,bitsPerPixel,bytesPerRow,colorSpaceRef,bitmapInfo,provider,NULL,NO,renderingIntent);

uint32_t *pixels = (uint32_t *)malloc(bufferLenght);
CGContextRef context = CGBitmapContextCreate(pixels, WIDTH, HEIGHT, 8, WIDTH*4, CGImageGetColorSpace(iref), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

CGContextTranslateCTM(context,0, size.height);
CGContextScaleCTM(context, 1.0, -1.0);

CGContextDrawImage(context, CGRectMake(0.0, 0.0, size.width, size.height), iref);
UIImage* screenshot = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];

UIGraphicsEndImageContext();

//free memory
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(provider);
CGImageRelease(iref);
CGContextRelease(context);
free(buffer);
free(pixels);

return screenshot;
}

@end

I use it like this from an UIViewController:

@interface
    UIImageView *screenShot;
    UIImage *tempImage;

-(void) deactivePage
{
// attach screenshot
tempImage = [self.stage getImageScreenshot];
screenShot = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,1024,768)];
screenShot.image = tempImage;  
[self.view addSubview:screenShot];
}

- (void)dealloc 
{
screenShot.image = nil;
[screenShot removeFromSuperview];
[screenShot release];
[super dealloc];
}

The UIViewController is released and deallocated aprox. 5 Seconds after the "deactivePage" function is called. The Screenshot is used for a View Transition.

Taking Screenshots works like a charm, but with every Screenshot my App is growing around 10 MBs So i can do this around 15 Times till the app crashes.

So where is the leak? I am stuck.. :-(


Solution

  • In the getImageScreenshot function you do this:

    UIImage* screenshot = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];
    

    which creates a CGImageRef and then creates (autorelease) an UIImage from it. What happens here is that this CGImageRef remains alive and is never released, so it's leaking.

    What you should do, instead, is this:

    
    CGImageRef myCGImage = CGBitmapContextCreateImage(context);
    UIImage* screenshot = [UIImage imageWithCGImage:myCGImage];
    CGImageRelease(myCGImage);
    

    Have you tried to see it using Instruments (Leaks or Heapshots)? you should see these CGImareRef elements still alive.