javorosas
javorosas

Reputation: 769

"Potential leak of an object" when using Core Graphics

My app is drawing a shadow using the following code:

-(void)drawShadow:(CGContextRef)context rect:(CGRect)rect{

    CGContextSaveGState(context);

    //Set color of current context
    [[UIColor blackColor] set];

    //Set shadow and color of shadow
    CGContextSetShadowWithColor(context, CGSizeMake(0, 2), 3, [[UIColor colorWithWhite:0 alpha:0.5] CGColor]);

    CGContextFillEllipseInRect(context, rect);

    CGContextClipToMask(context, rect, CGBitmapContextCreateImage(context));

    CGContextRestoreGState(context); // Warning shows in this line

}

When I run Product > Analyze, it marks the last instruction of this block with the message: "Potential leak of an object". When I remove that line, it shows the same message but in the closing bracket.

Any idea of what I'm doing wrong?

Thanks

Upvotes: 1

Views: 94

Answers (2)

javorosas
javorosas

Reputation: 769

chedabob's answer worked! Here's the final code:

-(void)drawShadow:(CGContextRef)context rect:(CGRect)rect{
    CGContextSaveGState(context);
    //Set color of current context
    [[UIColor blackColor] set];
    //Set shadow and color of shadow
    CGContextSetShadowWithColor(context, CGSizeMake(0, 2), 3, [[UIColor colorWithWhite:0 alpha:0.5] CGColor]);
    CGContextFillEllipseInRect(context, rect);

    CGImageRef bitmap = CGBitmapContextCreateImage(context);
    CGContextClipToMask(context, rect, bitmap);
    CGContextRestoreGState(context);

    CGImageRelease(bitmap);
}

Upvotes: 0

chedabob
chedabob

Reputation: 5881

I think it's the CGBitmapContextCreateImage that is leaking.

Try assigning it to a local variable and then calling CGImageRelease with it once you've clipped to mask.

Another iPhone - CGBitmapContextCreateImage Leak

Upvotes: 2

Related Questions